r8152.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593
  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. #include <uapi/linux/mdio.h>
  25. #include <linux/mdio.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/suspend.h>
  28. #include <linux/acpi.h>
  29. /* Information for net-next */
  30. #define NETNEXT_VERSION "08"
  31. /* Information for net */
  32. #define NET_VERSION "9"
  33. #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
  34. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  35. #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
  36. #define MODULENAME "r8152"
  37. #define R8152_PHY_ID 32
  38. #define PLA_IDR 0xc000
  39. #define PLA_RCR 0xc010
  40. #define PLA_RMS 0xc016
  41. #define PLA_RXFIFO_CTRL0 0xc0a0
  42. #define PLA_RXFIFO_CTRL1 0xc0a4
  43. #define PLA_RXFIFO_CTRL2 0xc0a8
  44. #define PLA_DMY_REG0 0xc0b0
  45. #define PLA_FMC 0xc0b4
  46. #define PLA_CFG_WOL 0xc0b6
  47. #define PLA_TEREDO_CFG 0xc0bc
  48. #define PLA_MAR 0xcd00
  49. #define PLA_BACKUP 0xd000
  50. #define PAL_BDC_CR 0xd1a0
  51. #define PLA_TEREDO_TIMER 0xd2cc
  52. #define PLA_REALWOW_TIMER 0xd2e8
  53. #define PLA_LEDSEL 0xdd90
  54. #define PLA_LED_FEATURE 0xdd92
  55. #define PLA_PHYAR 0xde00
  56. #define PLA_BOOT_CTRL 0xe004
  57. #define PLA_GPHY_INTR_IMR 0xe022
  58. #define PLA_EEE_CR 0xe040
  59. #define PLA_EEEP_CR 0xe080
  60. #define PLA_MAC_PWR_CTRL 0xe0c0
  61. #define PLA_MAC_PWR_CTRL2 0xe0ca
  62. #define PLA_MAC_PWR_CTRL3 0xe0cc
  63. #define PLA_MAC_PWR_CTRL4 0xe0ce
  64. #define PLA_WDT6_CTRL 0xe428
  65. #define PLA_TCR0 0xe610
  66. #define PLA_TCR1 0xe612
  67. #define PLA_MTPS 0xe615
  68. #define PLA_TXFIFO_CTRL 0xe618
  69. #define PLA_RSTTALLY 0xe800
  70. #define PLA_CR 0xe813
  71. #define PLA_CRWECR 0xe81c
  72. #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
  73. #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
  74. #define PLA_CONFIG5 0xe822
  75. #define PLA_PHY_PWR 0xe84c
  76. #define PLA_OOB_CTRL 0xe84f
  77. #define PLA_CPCR 0xe854
  78. #define PLA_MISC_0 0xe858
  79. #define PLA_MISC_1 0xe85a
  80. #define PLA_OCP_GPHY_BASE 0xe86c
  81. #define PLA_TALLYCNT 0xe890
  82. #define PLA_SFF_STS_7 0xe8de
  83. #define PLA_PHYSTATUS 0xe908
  84. #define PLA_BP_BA 0xfc26
  85. #define PLA_BP_0 0xfc28
  86. #define PLA_BP_1 0xfc2a
  87. #define PLA_BP_2 0xfc2c
  88. #define PLA_BP_3 0xfc2e
  89. #define PLA_BP_4 0xfc30
  90. #define PLA_BP_5 0xfc32
  91. #define PLA_BP_6 0xfc34
  92. #define PLA_BP_7 0xfc36
  93. #define PLA_BP_EN 0xfc38
  94. #define USB_USB2PHY 0xb41e
  95. #define USB_SSPHYLINK2 0xb428
  96. #define USB_U2P3_CTRL 0xb460
  97. #define USB_CSR_DUMMY1 0xb464
  98. #define USB_CSR_DUMMY2 0xb466
  99. #define USB_DEV_STAT 0xb808
  100. #define USB_CONNECT_TIMER 0xcbf8
  101. #define USB_BURST_SIZE 0xcfc0
  102. #define USB_USB_CTRL 0xd406
  103. #define USB_PHY_CTRL 0xd408
  104. #define USB_TX_AGG 0xd40a
  105. #define USB_RX_BUF_TH 0xd40c
  106. #define USB_USB_TIMER 0xd428
  107. #define USB_RX_EARLY_TIMEOUT 0xd42c
  108. #define USB_RX_EARLY_SIZE 0xd42e
  109. #define USB_PM_CTRL_STATUS 0xd432
  110. #define USB_TX_DMA 0xd434
  111. #define USB_TOLERANCE 0xd490
  112. #define USB_LPM_CTRL 0xd41a
  113. #define USB_BMU_RESET 0xd4b0
  114. #define USB_UPS_CTRL 0xd800
  115. #define USB_MISC_0 0xd81a
  116. #define USB_POWER_CUT 0xd80a
  117. #define USB_AFE_CTRL2 0xd824
  118. #define USB_WDT11_CTRL 0xe43c
  119. #define USB_BP_BA 0xfc26
  120. #define USB_BP_0 0xfc28
  121. #define USB_BP_1 0xfc2a
  122. #define USB_BP_2 0xfc2c
  123. #define USB_BP_3 0xfc2e
  124. #define USB_BP_4 0xfc30
  125. #define USB_BP_5 0xfc32
  126. #define USB_BP_6 0xfc34
  127. #define USB_BP_7 0xfc36
  128. #define USB_BP_EN 0xfc38
  129. /* OCP Registers */
  130. #define OCP_ALDPS_CONFIG 0x2010
  131. #define OCP_EEE_CONFIG1 0x2080
  132. #define OCP_EEE_CONFIG2 0x2092
  133. #define OCP_EEE_CONFIG3 0x2094
  134. #define OCP_BASE_MII 0xa400
  135. #define OCP_EEE_AR 0xa41a
  136. #define OCP_EEE_DATA 0xa41c
  137. #define OCP_PHY_STATUS 0xa420
  138. #define OCP_POWER_CFG 0xa430
  139. #define OCP_EEE_CFG 0xa432
  140. #define OCP_SRAM_ADDR 0xa436
  141. #define OCP_SRAM_DATA 0xa438
  142. #define OCP_DOWN_SPEED 0xa442
  143. #define OCP_EEE_ABLE 0xa5c4
  144. #define OCP_EEE_ADV 0xa5d0
  145. #define OCP_EEE_LPABLE 0xa5d2
  146. #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
  147. #define OCP_ADC_CFG 0xbc06
  148. /* SRAM Register */
  149. #define SRAM_LPF_CFG 0x8012
  150. #define SRAM_10M_AMP1 0x8080
  151. #define SRAM_10M_AMP2 0x8082
  152. #define SRAM_IMPEDANCE 0x8084
  153. /* PLA_RCR */
  154. #define RCR_AAP 0x00000001
  155. #define RCR_APM 0x00000002
  156. #define RCR_AM 0x00000004
  157. #define RCR_AB 0x00000008
  158. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  159. /* PLA_RXFIFO_CTRL0 */
  160. #define RXFIFO_THR1_NORMAL 0x00080002
  161. #define RXFIFO_THR1_OOB 0x01800003
  162. /* PLA_RXFIFO_CTRL1 */
  163. #define RXFIFO_THR2_FULL 0x00000060
  164. #define RXFIFO_THR2_HIGH 0x00000038
  165. #define RXFIFO_THR2_OOB 0x0000004a
  166. #define RXFIFO_THR2_NORMAL 0x00a0
  167. /* PLA_RXFIFO_CTRL2 */
  168. #define RXFIFO_THR3_FULL 0x00000078
  169. #define RXFIFO_THR3_HIGH 0x00000048
  170. #define RXFIFO_THR3_OOB 0x0000005a
  171. #define RXFIFO_THR3_NORMAL 0x0110
  172. /* PLA_TXFIFO_CTRL */
  173. #define TXFIFO_THR_NORMAL 0x00400008
  174. #define TXFIFO_THR_NORMAL2 0x01000008
  175. /* PLA_DMY_REG0 */
  176. #define ECM_ALDPS 0x0002
  177. /* PLA_FMC */
  178. #define FMC_FCR_MCU_EN 0x0001
  179. /* PLA_EEEP_CR */
  180. #define EEEP_CR_EEEP_TX 0x0002
  181. /* PLA_WDT6_CTRL */
  182. #define WDT6_SET_MODE 0x0010
  183. /* PLA_TCR0 */
  184. #define TCR0_TX_EMPTY 0x0800
  185. #define TCR0_AUTO_FIFO 0x0080
  186. /* PLA_TCR1 */
  187. #define VERSION_MASK 0x7cf0
  188. /* PLA_MTPS */
  189. #define MTPS_JUMBO (12 * 1024 / 64)
  190. #define MTPS_DEFAULT (6 * 1024 / 64)
  191. /* PLA_RSTTALLY */
  192. #define TALLY_RESET 0x0001
  193. /* PLA_CR */
  194. #define CR_RST 0x10
  195. #define CR_RE 0x08
  196. #define CR_TE 0x04
  197. /* PLA_CRWECR */
  198. #define CRWECR_NORAML 0x00
  199. #define CRWECR_CONFIG 0xc0
  200. /* PLA_OOB_CTRL */
  201. #define NOW_IS_OOB 0x80
  202. #define TXFIFO_EMPTY 0x20
  203. #define RXFIFO_EMPTY 0x10
  204. #define LINK_LIST_READY 0x02
  205. #define DIS_MCU_CLROOB 0x01
  206. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  207. /* PLA_MISC_1 */
  208. #define RXDY_GATED_EN 0x0008
  209. /* PLA_SFF_STS_7 */
  210. #define RE_INIT_LL 0x8000
  211. #define MCU_BORW_EN 0x4000
  212. /* PLA_CPCR */
  213. #define CPCR_RX_VLAN 0x0040
  214. /* PLA_CFG_WOL */
  215. #define MAGIC_EN 0x0001
  216. /* PLA_TEREDO_CFG */
  217. #define TEREDO_SEL 0x8000
  218. #define TEREDO_WAKE_MASK 0x7f00
  219. #define TEREDO_RS_EVENT_MASK 0x00fe
  220. #define OOB_TEREDO_EN 0x0001
  221. /* PAL_BDC_CR */
  222. #define ALDPS_PROXY_MODE 0x0001
  223. /* PLA_CONFIG34 */
  224. #define LINK_ON_WAKE_EN 0x0010
  225. #define LINK_OFF_WAKE_EN 0x0008
  226. /* PLA_CONFIG5 */
  227. #define BWF_EN 0x0040
  228. #define MWF_EN 0x0020
  229. #define UWF_EN 0x0010
  230. #define LAN_WAKE_EN 0x0002
  231. /* PLA_LED_FEATURE */
  232. #define LED_MODE_MASK 0x0700
  233. /* PLA_PHY_PWR */
  234. #define TX_10M_IDLE_EN 0x0080
  235. #define PFM_PWM_SWITCH 0x0040
  236. /* PLA_MAC_PWR_CTRL */
  237. #define D3_CLK_GATED_EN 0x00004000
  238. #define MCU_CLK_RATIO 0x07010f07
  239. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  240. #define ALDPS_SPDWN_RATIO 0x0f87
  241. /* PLA_MAC_PWR_CTRL2 */
  242. #define EEE_SPDWN_RATIO 0x8007
  243. /* PLA_MAC_PWR_CTRL3 */
  244. #define PKT_AVAIL_SPDWN_EN 0x0100
  245. #define SUSPEND_SPDWN_EN 0x0004
  246. #define U1U2_SPDWN_EN 0x0002
  247. #define L1_SPDWN_EN 0x0001
  248. /* PLA_MAC_PWR_CTRL4 */
  249. #define PWRSAVE_SPDWN_EN 0x1000
  250. #define RXDV_SPDWN_EN 0x0800
  251. #define TX10MIDLE_EN 0x0100
  252. #define TP100_SPDWN_EN 0x0020
  253. #define TP500_SPDWN_EN 0x0010
  254. #define TP1000_SPDWN_EN 0x0008
  255. #define EEE_SPDWN_EN 0x0001
  256. /* PLA_GPHY_INTR_IMR */
  257. #define GPHY_STS_MSK 0x0001
  258. #define SPEED_DOWN_MSK 0x0002
  259. #define SPDWN_RXDV_MSK 0x0004
  260. #define SPDWN_LINKCHG_MSK 0x0008
  261. /* PLA_PHYAR */
  262. #define PHYAR_FLAG 0x80000000
  263. /* PLA_EEE_CR */
  264. #define EEE_RX_EN 0x0001
  265. #define EEE_TX_EN 0x0002
  266. /* PLA_BOOT_CTRL */
  267. #define AUTOLOAD_DONE 0x0002
  268. /* USB_USB2PHY */
  269. #define USB2PHY_SUSPEND 0x0001
  270. #define USB2PHY_L1 0x0002
  271. /* USB_SSPHYLINK2 */
  272. #define pwd_dn_scale_mask 0x3ffe
  273. #define pwd_dn_scale(x) ((x) << 1)
  274. /* USB_CSR_DUMMY1 */
  275. #define DYNAMIC_BURST 0x0001
  276. /* USB_CSR_DUMMY2 */
  277. #define EP4_FULL_FC 0x0001
  278. /* USB_DEV_STAT */
  279. #define STAT_SPEED_MASK 0x0006
  280. #define STAT_SPEED_HIGH 0x0000
  281. #define STAT_SPEED_FULL 0x0002
  282. /* USB_TX_AGG */
  283. #define TX_AGG_MAX_THRESHOLD 0x03
  284. /* USB_RX_BUF_TH */
  285. #define RX_THR_SUPPER 0x0c350180
  286. #define RX_THR_HIGH 0x7a120180
  287. #define RX_THR_SLOW 0xffff0180
  288. /* USB_TX_DMA */
  289. #define TEST_MODE_DISABLE 0x00000001
  290. #define TX_SIZE_ADJUST1 0x00000100
  291. /* USB_BMU_RESET */
  292. #define BMU_RESET_EP_IN 0x01
  293. #define BMU_RESET_EP_OUT 0x02
  294. /* USB_UPS_CTRL */
  295. #define POWER_CUT 0x0100
  296. /* USB_PM_CTRL_STATUS */
  297. #define RESUME_INDICATE 0x0001
  298. /* USB_USB_CTRL */
  299. #define RX_AGG_DISABLE 0x0010
  300. #define RX_ZERO_EN 0x0080
  301. /* USB_U2P3_CTRL */
  302. #define U2P3_ENABLE 0x0001
  303. /* USB_POWER_CUT */
  304. #define PWR_EN 0x0001
  305. #define PHASE2_EN 0x0008
  306. /* USB_MISC_0 */
  307. #define PCUT_STATUS 0x0001
  308. /* USB_RX_EARLY_TIMEOUT */
  309. #define COALESCE_SUPER 85000U
  310. #define COALESCE_HIGH 250000U
  311. #define COALESCE_SLOW 524280U
  312. /* USB_WDT11_CTRL */
  313. #define TIMER11_EN 0x0001
  314. /* USB_LPM_CTRL */
  315. /* bit 4 ~ 5: fifo empty boundary */
  316. #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
  317. /* bit 2 ~ 3: LMP timer */
  318. #define LPM_TIMER_MASK 0x0c
  319. #define LPM_TIMER_500MS 0x04 /* 500 ms */
  320. #define LPM_TIMER_500US 0x0c /* 500 us */
  321. #define ROK_EXIT_LPM 0x02
  322. /* USB_AFE_CTRL2 */
  323. #define SEN_VAL_MASK 0xf800
  324. #define SEN_VAL_NORMAL 0xa000
  325. #define SEL_RXIDLE 0x0100
  326. /* OCP_ALDPS_CONFIG */
  327. #define ENPWRSAVE 0x8000
  328. #define ENPDNPS 0x0200
  329. #define LINKENA 0x0100
  330. #define DIS_SDSAVE 0x0010
  331. /* OCP_PHY_STATUS */
  332. #define PHY_STAT_MASK 0x0007
  333. #define PHY_STAT_LAN_ON 3
  334. #define PHY_STAT_PWRDN 5
  335. /* OCP_POWER_CFG */
  336. #define EEE_CLKDIV_EN 0x8000
  337. #define EN_ALDPS 0x0004
  338. #define EN_10M_PLLOFF 0x0001
  339. /* OCP_EEE_CONFIG1 */
  340. #define RG_TXLPI_MSK_HFDUP 0x8000
  341. #define RG_MATCLR_EN 0x4000
  342. #define EEE_10_CAP 0x2000
  343. #define EEE_NWAY_EN 0x1000
  344. #define TX_QUIET_EN 0x0200
  345. #define RX_QUIET_EN 0x0100
  346. #define sd_rise_time_mask 0x0070
  347. #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
  348. #define RG_RXLPI_MSK_HFDUP 0x0008
  349. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  350. /* OCP_EEE_CONFIG2 */
  351. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  352. #define RG_DACQUIET_EN 0x0400
  353. #define RG_LDVQUIET_EN 0x0200
  354. #define RG_CKRSEL 0x0020
  355. #define RG_EEEPRG_EN 0x0010
  356. /* OCP_EEE_CONFIG3 */
  357. #define fast_snr_mask 0xff80
  358. #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
  359. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  360. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  361. /* OCP_EEE_AR */
  362. /* bit[15:14] function */
  363. #define FUN_ADDR 0x0000
  364. #define FUN_DATA 0x4000
  365. /* bit[4:0] device addr */
  366. /* OCP_EEE_CFG */
  367. #define CTAP_SHORT_EN 0x0040
  368. #define EEE10_EN 0x0010
  369. /* OCP_DOWN_SPEED */
  370. #define EN_10M_BGOFF 0x0080
  371. /* OCP_PHY_STATE */
  372. #define TXDIS_STATE 0x01
  373. #define ABD_STATE 0x02
  374. /* OCP_ADC_CFG */
  375. #define CKADSEL_L 0x0100
  376. #define ADC_EN 0x0080
  377. #define EN_EMI_L 0x0040
  378. /* SRAM_LPF_CFG */
  379. #define LPF_AUTO_TUNE 0x8000
  380. /* SRAM_10M_AMP1 */
  381. #define GDAC_IB_UPALL 0x0008
  382. /* SRAM_10M_AMP2 */
  383. #define AMP_DN 0x0200
  384. /* SRAM_IMPEDANCE */
  385. #define RX_DRIVING_MASK 0x6000
  386. /* MAC PASSTHRU */
  387. #define AD_MASK 0xfee0
  388. #define EFUSE 0xcfdb
  389. #define PASS_THRU_MASK 0x1
  390. enum rtl_register_content {
  391. _1000bps = 0x10,
  392. _100bps = 0x08,
  393. _10bps = 0x04,
  394. LINK_STATUS = 0x02,
  395. FULL_DUP = 0x01,
  396. };
  397. #define RTL8152_MAX_TX 4
  398. #define RTL8152_MAX_RX 10
  399. #define INTBUFSIZE 2
  400. #define CRC_SIZE 4
  401. #define TX_ALIGN 4
  402. #define RX_ALIGN 8
  403. #define INTR_LINK 0x0004
  404. #define RTL8152_REQT_READ 0xc0
  405. #define RTL8152_REQT_WRITE 0x40
  406. #define RTL8152_REQ_GET_REGS 0x05
  407. #define RTL8152_REQ_SET_REGS 0x05
  408. #define BYTE_EN_DWORD 0xff
  409. #define BYTE_EN_WORD 0x33
  410. #define BYTE_EN_BYTE 0x11
  411. #define BYTE_EN_SIX_BYTES 0x3f
  412. #define BYTE_EN_START_MASK 0x0f
  413. #define BYTE_EN_END_MASK 0xf0
  414. #define RTL8153_MAX_PACKET 9216 /* 9K */
  415. #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
  416. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
  417. #define RTL8153_RMS RTL8153_MAX_PACKET
  418. #define RTL8152_TX_TIMEOUT (5 * HZ)
  419. #define RTL8152_NAPI_WEIGHT 64
  420. #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + CRC_SIZE + \
  421. sizeof(struct rx_desc) + RX_ALIGN)
  422. /* rtl8152 flags */
  423. enum rtl8152_flags {
  424. RTL8152_UNPLUG = 0,
  425. RTL8152_SET_RX_MODE,
  426. WORK_ENABLE,
  427. RTL8152_LINK_CHG,
  428. SELECTIVE_SUSPEND,
  429. PHY_RESET,
  430. SCHEDULE_NAPI,
  431. };
  432. /* Define these values to match your device */
  433. #define VENDOR_ID_REALTEK 0x0bda
  434. #define VENDOR_ID_MICROSOFT 0x045e
  435. #define VENDOR_ID_SAMSUNG 0x04e8
  436. #define VENDOR_ID_LENOVO 0x17ef
  437. #define VENDOR_ID_NVIDIA 0x0955
  438. #define MCU_TYPE_PLA 0x0100
  439. #define MCU_TYPE_USB 0x0000
  440. struct tally_counter {
  441. __le64 tx_packets;
  442. __le64 rx_packets;
  443. __le64 tx_errors;
  444. __le32 rx_errors;
  445. __le16 rx_missed;
  446. __le16 align_errors;
  447. __le32 tx_one_collision;
  448. __le32 tx_multi_collision;
  449. __le64 rx_unicast;
  450. __le64 rx_broadcast;
  451. __le32 rx_multicast;
  452. __le16 tx_aborted;
  453. __le16 tx_underrun;
  454. };
  455. struct rx_desc {
  456. __le32 opts1;
  457. #define RX_LEN_MASK 0x7fff
  458. __le32 opts2;
  459. #define RD_UDP_CS BIT(23)
  460. #define RD_TCP_CS BIT(22)
  461. #define RD_IPV6_CS BIT(20)
  462. #define RD_IPV4_CS BIT(19)
  463. __le32 opts3;
  464. #define IPF BIT(23) /* IP checksum fail */
  465. #define UDPF BIT(22) /* UDP checksum fail */
  466. #define TCPF BIT(21) /* TCP checksum fail */
  467. #define RX_VLAN_TAG BIT(16)
  468. __le32 opts4;
  469. __le32 opts5;
  470. __le32 opts6;
  471. };
  472. struct tx_desc {
  473. __le32 opts1;
  474. #define TX_FS BIT(31) /* First segment of a packet */
  475. #define TX_LS BIT(30) /* Final segment of a packet */
  476. #define GTSENDV4 BIT(28)
  477. #define GTSENDV6 BIT(27)
  478. #define GTTCPHO_SHIFT 18
  479. #define GTTCPHO_MAX 0x7fU
  480. #define TX_LEN_MAX 0x3ffffU
  481. __le32 opts2;
  482. #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
  483. #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
  484. #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
  485. #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
  486. #define MSS_SHIFT 17
  487. #define MSS_MAX 0x7ffU
  488. #define TCPHO_SHIFT 17
  489. #define TCPHO_MAX 0x7ffU
  490. #define TX_VLAN_TAG BIT(16)
  491. };
  492. struct r8152;
  493. struct rx_agg {
  494. struct list_head list;
  495. struct urb *urb;
  496. struct r8152 *context;
  497. void *buffer;
  498. void *head;
  499. };
  500. struct tx_agg {
  501. struct list_head list;
  502. struct urb *urb;
  503. struct r8152 *context;
  504. void *buffer;
  505. void *head;
  506. u32 skb_num;
  507. u32 skb_len;
  508. };
  509. struct r8152 {
  510. unsigned long flags;
  511. struct usb_device *udev;
  512. struct napi_struct napi;
  513. struct usb_interface *intf;
  514. struct net_device *netdev;
  515. struct urb *intr_urb;
  516. struct tx_agg tx_info[RTL8152_MAX_TX];
  517. struct rx_agg rx_info[RTL8152_MAX_RX];
  518. struct list_head rx_done, tx_free;
  519. struct sk_buff_head tx_queue, rx_queue;
  520. spinlock_t rx_lock, tx_lock;
  521. struct delayed_work schedule, hw_phy_work;
  522. struct mii_if_info mii;
  523. struct mutex control; /* use for hw setting */
  524. #ifdef CONFIG_PM_SLEEP
  525. struct notifier_block pm_notifier;
  526. #endif
  527. struct rtl_ops {
  528. void (*init)(struct r8152 *);
  529. int (*enable)(struct r8152 *);
  530. void (*disable)(struct r8152 *);
  531. void (*up)(struct r8152 *);
  532. void (*down)(struct r8152 *);
  533. void (*unload)(struct r8152 *);
  534. int (*eee_get)(struct r8152 *, struct ethtool_eee *);
  535. int (*eee_set)(struct r8152 *, struct ethtool_eee *);
  536. bool (*in_nway)(struct r8152 *);
  537. void (*hw_phy_cfg)(struct r8152 *);
  538. void (*autosuspend_en)(struct r8152 *tp, bool enable);
  539. } rtl_ops;
  540. int intr_interval;
  541. u32 saved_wolopts;
  542. u32 msg_enable;
  543. u32 tx_qlen;
  544. u32 coalesce;
  545. u16 ocp_base;
  546. u16 speed;
  547. u8 *intr_buff;
  548. u8 version;
  549. u8 duplex;
  550. u8 autoneg;
  551. };
  552. enum rtl_version {
  553. RTL_VER_UNKNOWN = 0,
  554. RTL_VER_01,
  555. RTL_VER_02,
  556. RTL_VER_03,
  557. RTL_VER_04,
  558. RTL_VER_05,
  559. RTL_VER_06,
  560. RTL_VER_MAX
  561. };
  562. enum tx_csum_stat {
  563. TX_CSUM_SUCCESS = 0,
  564. TX_CSUM_TSO,
  565. TX_CSUM_NONE
  566. };
  567. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  568. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  569. */
  570. static const int multicast_filter_limit = 32;
  571. static unsigned int agg_buf_sz = 16384;
  572. #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
  573. VLAN_ETH_HLEN - VLAN_HLEN)
  574. static
  575. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  576. {
  577. int ret;
  578. void *tmp;
  579. tmp = kmalloc(size, GFP_KERNEL);
  580. if (!tmp)
  581. return -ENOMEM;
  582. ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  583. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  584. value, index, tmp, size, 500);
  585. memcpy(data, tmp, size);
  586. kfree(tmp);
  587. return ret;
  588. }
  589. static
  590. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  591. {
  592. int ret;
  593. void *tmp;
  594. tmp = kmemdup(data, size, GFP_KERNEL);
  595. if (!tmp)
  596. return -ENOMEM;
  597. ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  598. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  599. value, index, tmp, size, 500);
  600. kfree(tmp);
  601. return ret;
  602. }
  603. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  604. void *data, u16 type)
  605. {
  606. u16 limit = 64;
  607. int ret = 0;
  608. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  609. return -ENODEV;
  610. /* both size and indix must be 4 bytes align */
  611. if ((size & 3) || !size || (index & 3) || !data)
  612. return -EPERM;
  613. if ((u32)index + (u32)size > 0xffff)
  614. return -EPERM;
  615. while (size) {
  616. if (size > limit) {
  617. ret = get_registers(tp, index, type, limit, data);
  618. if (ret < 0)
  619. break;
  620. index += limit;
  621. data += limit;
  622. size -= limit;
  623. } else {
  624. ret = get_registers(tp, index, type, size, data);
  625. if (ret < 0)
  626. break;
  627. index += size;
  628. data += size;
  629. size = 0;
  630. break;
  631. }
  632. }
  633. if (ret == -ENODEV)
  634. set_bit(RTL8152_UNPLUG, &tp->flags);
  635. return ret;
  636. }
  637. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  638. u16 size, void *data, u16 type)
  639. {
  640. int ret;
  641. u16 byteen_start, byteen_end, byen;
  642. u16 limit = 512;
  643. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  644. return -ENODEV;
  645. /* both size and indix must be 4 bytes align */
  646. if ((size & 3) || !size || (index & 3) || !data)
  647. return -EPERM;
  648. if ((u32)index + (u32)size > 0xffff)
  649. return -EPERM;
  650. byteen_start = byteen & BYTE_EN_START_MASK;
  651. byteen_end = byteen & BYTE_EN_END_MASK;
  652. byen = byteen_start | (byteen_start << 4);
  653. ret = set_registers(tp, index, type | byen, 4, data);
  654. if (ret < 0)
  655. goto error1;
  656. index += 4;
  657. data += 4;
  658. size -= 4;
  659. if (size) {
  660. size -= 4;
  661. while (size) {
  662. if (size > limit) {
  663. ret = set_registers(tp, index,
  664. type | BYTE_EN_DWORD,
  665. limit, data);
  666. if (ret < 0)
  667. goto error1;
  668. index += limit;
  669. data += limit;
  670. size -= limit;
  671. } else {
  672. ret = set_registers(tp, index,
  673. type | BYTE_EN_DWORD,
  674. size, data);
  675. if (ret < 0)
  676. goto error1;
  677. index += size;
  678. data += size;
  679. size = 0;
  680. break;
  681. }
  682. }
  683. byen = byteen_end | (byteen_end >> 4);
  684. ret = set_registers(tp, index, type | byen, 4, data);
  685. if (ret < 0)
  686. goto error1;
  687. }
  688. error1:
  689. if (ret == -ENODEV)
  690. set_bit(RTL8152_UNPLUG, &tp->flags);
  691. return ret;
  692. }
  693. static inline
  694. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  695. {
  696. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  697. }
  698. static inline
  699. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  700. {
  701. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  702. }
  703. static inline
  704. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  705. {
  706. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  707. }
  708. static inline
  709. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  710. {
  711. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  712. }
  713. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  714. {
  715. __le32 data;
  716. generic_ocp_read(tp, index, sizeof(data), &data, type);
  717. return __le32_to_cpu(data);
  718. }
  719. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  720. {
  721. __le32 tmp = __cpu_to_le32(data);
  722. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  723. }
  724. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  725. {
  726. u32 data;
  727. __le32 tmp;
  728. u8 shift = index & 2;
  729. index &= ~3;
  730. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  731. data = __le32_to_cpu(tmp);
  732. data >>= (shift * 8);
  733. data &= 0xffff;
  734. return (u16)data;
  735. }
  736. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  737. {
  738. u32 mask = 0xffff;
  739. __le32 tmp;
  740. u16 byen = BYTE_EN_WORD;
  741. u8 shift = index & 2;
  742. data &= mask;
  743. if (index & 2) {
  744. byen <<= shift;
  745. mask <<= (shift * 8);
  746. data <<= (shift * 8);
  747. index &= ~3;
  748. }
  749. tmp = __cpu_to_le32(data);
  750. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  751. }
  752. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  753. {
  754. u32 data;
  755. __le32 tmp;
  756. u8 shift = index & 3;
  757. index &= ~3;
  758. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  759. data = __le32_to_cpu(tmp);
  760. data >>= (shift * 8);
  761. data &= 0xff;
  762. return (u8)data;
  763. }
  764. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  765. {
  766. u32 mask = 0xff;
  767. __le32 tmp;
  768. u16 byen = BYTE_EN_BYTE;
  769. u8 shift = index & 3;
  770. data &= mask;
  771. if (index & 3) {
  772. byen <<= shift;
  773. mask <<= (shift * 8);
  774. data <<= (shift * 8);
  775. index &= ~3;
  776. }
  777. tmp = __cpu_to_le32(data);
  778. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  779. }
  780. static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
  781. {
  782. u16 ocp_base, ocp_index;
  783. ocp_base = addr & 0xf000;
  784. if (ocp_base != tp->ocp_base) {
  785. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  786. tp->ocp_base = ocp_base;
  787. }
  788. ocp_index = (addr & 0x0fff) | 0xb000;
  789. return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
  790. }
  791. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  792. {
  793. u16 ocp_base, ocp_index;
  794. ocp_base = addr & 0xf000;
  795. if (ocp_base != tp->ocp_base) {
  796. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  797. tp->ocp_base = ocp_base;
  798. }
  799. ocp_index = (addr & 0x0fff) | 0xb000;
  800. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  801. }
  802. static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  803. {
  804. ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
  805. }
  806. static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  807. {
  808. return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
  809. }
  810. static void sram_write(struct r8152 *tp, u16 addr, u16 data)
  811. {
  812. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  813. ocp_reg_write(tp, OCP_SRAM_DATA, data);
  814. }
  815. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  816. {
  817. struct r8152 *tp = netdev_priv(netdev);
  818. int ret;
  819. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  820. return -ENODEV;
  821. if (phy_id != R8152_PHY_ID)
  822. return -EINVAL;
  823. ret = r8152_mdio_read(tp, reg);
  824. return ret;
  825. }
  826. static
  827. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  828. {
  829. struct r8152 *tp = netdev_priv(netdev);
  830. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  831. return;
  832. if (phy_id != R8152_PHY_ID)
  833. return;
  834. r8152_mdio_write(tp, reg, val);
  835. }
  836. static int
  837. r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
  838. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  839. {
  840. struct r8152 *tp = netdev_priv(netdev);
  841. struct sockaddr *addr = p;
  842. int ret = -EADDRNOTAVAIL;
  843. if (!is_valid_ether_addr(addr->sa_data))
  844. goto out1;
  845. ret = usb_autopm_get_interface(tp->intf);
  846. if (ret < 0)
  847. goto out1;
  848. mutex_lock(&tp->control);
  849. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  850. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  851. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  852. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  853. mutex_unlock(&tp->control);
  854. usb_autopm_put_interface(tp->intf);
  855. out1:
  856. return ret;
  857. }
  858. /* Devices containing RTL8153-AD can support a persistent
  859. * host system provided MAC address.
  860. * Examples of this are Dell TB15 and Dell WD15 docks
  861. */
  862. static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
  863. {
  864. acpi_status status;
  865. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  866. union acpi_object *obj;
  867. int ret = -EINVAL;
  868. u32 ocp_data;
  869. unsigned char buf[6];
  870. /* test for -AD variant of RTL8153 */
  871. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  872. if ((ocp_data & AD_MASK) != 0x1000)
  873. return -ENODEV;
  874. /* test for MAC address pass-through bit */
  875. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
  876. if ((ocp_data & PASS_THRU_MASK) != 1)
  877. return -ENODEV;
  878. /* returns _AUXMAC_#AABBCCDDEEFF# */
  879. status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
  880. obj = (union acpi_object *)buffer.pointer;
  881. if (!ACPI_SUCCESS(status))
  882. return -ENODEV;
  883. if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
  884. netif_warn(tp, probe, tp->netdev,
  885. "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
  886. obj->type, obj->string.length);
  887. goto amacout;
  888. }
  889. if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
  890. strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
  891. netif_warn(tp, probe, tp->netdev,
  892. "Invalid header when reading pass-thru MAC addr\n");
  893. goto amacout;
  894. }
  895. ret = hex2bin(buf, obj->string.pointer + 9, 6);
  896. if (!(ret == 0 && is_valid_ether_addr(buf))) {
  897. netif_warn(tp, probe, tp->netdev,
  898. "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
  899. ret, buf);
  900. ret = -EINVAL;
  901. goto amacout;
  902. }
  903. memcpy(sa->sa_data, buf, 6);
  904. ether_addr_copy(tp->netdev->dev_addr, sa->sa_data);
  905. netif_info(tp, probe, tp->netdev,
  906. "Using pass-thru MAC addr %pM\n", sa->sa_data);
  907. amacout:
  908. kfree(obj);
  909. return ret;
  910. }
  911. static int set_ethernet_addr(struct r8152 *tp)
  912. {
  913. struct net_device *dev = tp->netdev;
  914. struct sockaddr sa;
  915. int ret;
  916. if (tp->version == RTL_VER_01) {
  917. ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
  918. } else {
  919. /* if this is not an RTL8153-AD, no eFuse mac pass thru set,
  920. * or system doesn't provide valid _SB.AMAC this will be
  921. * be expected to non-zero
  922. */
  923. ret = vendor_mac_passthru_addr_read(tp, &sa);
  924. if (ret < 0)
  925. ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
  926. }
  927. if (ret < 0) {
  928. netif_err(tp, probe, dev, "Get ether addr fail\n");
  929. } else if (!is_valid_ether_addr(sa.sa_data)) {
  930. netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
  931. sa.sa_data);
  932. eth_hw_addr_random(dev);
  933. ether_addr_copy(sa.sa_data, dev->dev_addr);
  934. ret = rtl8152_set_mac_address(dev, &sa);
  935. netif_info(tp, probe, dev, "Random ether addr %pM\n",
  936. sa.sa_data);
  937. } else {
  938. if (tp->version == RTL_VER_01)
  939. ether_addr_copy(dev->dev_addr, sa.sa_data);
  940. else
  941. ret = rtl8152_set_mac_address(dev, &sa);
  942. }
  943. return ret;
  944. }
  945. static void read_bulk_callback(struct urb *urb)
  946. {
  947. struct net_device *netdev;
  948. int status = urb->status;
  949. struct rx_agg *agg;
  950. struct r8152 *tp;
  951. agg = urb->context;
  952. if (!agg)
  953. return;
  954. tp = agg->context;
  955. if (!tp)
  956. return;
  957. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  958. return;
  959. if (!test_bit(WORK_ENABLE, &tp->flags))
  960. return;
  961. netdev = tp->netdev;
  962. /* When link down, the driver would cancel all bulks. */
  963. /* This avoid the re-submitting bulk */
  964. if (!netif_carrier_ok(netdev))
  965. return;
  966. usb_mark_last_busy(tp->udev);
  967. switch (status) {
  968. case 0:
  969. if (urb->actual_length < ETH_ZLEN)
  970. break;
  971. spin_lock(&tp->rx_lock);
  972. list_add_tail(&agg->list, &tp->rx_done);
  973. spin_unlock(&tp->rx_lock);
  974. napi_schedule(&tp->napi);
  975. return;
  976. case -ESHUTDOWN:
  977. set_bit(RTL8152_UNPLUG, &tp->flags);
  978. netif_device_detach(tp->netdev);
  979. return;
  980. case -ENOENT:
  981. return; /* the urb is in unlink state */
  982. case -ETIME:
  983. if (net_ratelimit())
  984. netdev_warn(netdev, "maybe reset is needed?\n");
  985. break;
  986. default:
  987. if (net_ratelimit())
  988. netdev_warn(netdev, "Rx status %d\n", status);
  989. break;
  990. }
  991. r8152_submit_rx(tp, agg, GFP_ATOMIC);
  992. }
  993. static void write_bulk_callback(struct urb *urb)
  994. {
  995. struct net_device_stats *stats;
  996. struct net_device *netdev;
  997. struct tx_agg *agg;
  998. struct r8152 *tp;
  999. int status = urb->status;
  1000. agg = urb->context;
  1001. if (!agg)
  1002. return;
  1003. tp = agg->context;
  1004. if (!tp)
  1005. return;
  1006. netdev = tp->netdev;
  1007. stats = &netdev->stats;
  1008. if (status) {
  1009. if (net_ratelimit())
  1010. netdev_warn(netdev, "Tx status %d\n", status);
  1011. stats->tx_errors += agg->skb_num;
  1012. } else {
  1013. stats->tx_packets += agg->skb_num;
  1014. stats->tx_bytes += agg->skb_len;
  1015. }
  1016. spin_lock(&tp->tx_lock);
  1017. list_add_tail(&agg->list, &tp->tx_free);
  1018. spin_unlock(&tp->tx_lock);
  1019. usb_autopm_put_interface_async(tp->intf);
  1020. if (!netif_carrier_ok(netdev))
  1021. return;
  1022. if (!test_bit(WORK_ENABLE, &tp->flags))
  1023. return;
  1024. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1025. return;
  1026. if (!skb_queue_empty(&tp->tx_queue))
  1027. napi_schedule(&tp->napi);
  1028. }
  1029. static void intr_callback(struct urb *urb)
  1030. {
  1031. struct r8152 *tp;
  1032. __le16 *d;
  1033. int status = urb->status;
  1034. int res;
  1035. tp = urb->context;
  1036. if (!tp)
  1037. return;
  1038. if (!test_bit(WORK_ENABLE, &tp->flags))
  1039. return;
  1040. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1041. return;
  1042. switch (status) {
  1043. case 0: /* success */
  1044. break;
  1045. case -ECONNRESET: /* unlink */
  1046. case -ESHUTDOWN:
  1047. netif_device_detach(tp->netdev);
  1048. case -ENOENT:
  1049. case -EPROTO:
  1050. netif_info(tp, intr, tp->netdev,
  1051. "Stop submitting intr, status %d\n", status);
  1052. return;
  1053. case -EOVERFLOW:
  1054. netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
  1055. goto resubmit;
  1056. /* -EPIPE: should clear the halt */
  1057. default:
  1058. netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
  1059. goto resubmit;
  1060. }
  1061. d = urb->transfer_buffer;
  1062. if (INTR_LINK & __le16_to_cpu(d[0])) {
  1063. if (!netif_carrier_ok(tp->netdev)) {
  1064. set_bit(RTL8152_LINK_CHG, &tp->flags);
  1065. schedule_delayed_work(&tp->schedule, 0);
  1066. }
  1067. } else {
  1068. if (netif_carrier_ok(tp->netdev)) {
  1069. netif_stop_queue(tp->netdev);
  1070. set_bit(RTL8152_LINK_CHG, &tp->flags);
  1071. schedule_delayed_work(&tp->schedule, 0);
  1072. }
  1073. }
  1074. resubmit:
  1075. res = usb_submit_urb(urb, GFP_ATOMIC);
  1076. if (res == -ENODEV) {
  1077. set_bit(RTL8152_UNPLUG, &tp->flags);
  1078. netif_device_detach(tp->netdev);
  1079. } else if (res) {
  1080. netif_err(tp, intr, tp->netdev,
  1081. "can't resubmit intr, status %d\n", res);
  1082. }
  1083. }
  1084. static inline void *rx_agg_align(void *data)
  1085. {
  1086. return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
  1087. }
  1088. static inline void *tx_agg_align(void *data)
  1089. {
  1090. return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
  1091. }
  1092. static void free_all_mem(struct r8152 *tp)
  1093. {
  1094. int i;
  1095. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1096. usb_free_urb(tp->rx_info[i].urb);
  1097. tp->rx_info[i].urb = NULL;
  1098. kfree(tp->rx_info[i].buffer);
  1099. tp->rx_info[i].buffer = NULL;
  1100. tp->rx_info[i].head = NULL;
  1101. }
  1102. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1103. usb_free_urb(tp->tx_info[i].urb);
  1104. tp->tx_info[i].urb = NULL;
  1105. kfree(tp->tx_info[i].buffer);
  1106. tp->tx_info[i].buffer = NULL;
  1107. tp->tx_info[i].head = NULL;
  1108. }
  1109. usb_free_urb(tp->intr_urb);
  1110. tp->intr_urb = NULL;
  1111. kfree(tp->intr_buff);
  1112. tp->intr_buff = NULL;
  1113. }
  1114. static int alloc_all_mem(struct r8152 *tp)
  1115. {
  1116. struct net_device *netdev = tp->netdev;
  1117. struct usb_interface *intf = tp->intf;
  1118. struct usb_host_interface *alt = intf->cur_altsetting;
  1119. struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
  1120. struct urb *urb;
  1121. int node, i;
  1122. u8 *buf;
  1123. node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  1124. spin_lock_init(&tp->rx_lock);
  1125. spin_lock_init(&tp->tx_lock);
  1126. INIT_LIST_HEAD(&tp->tx_free);
  1127. INIT_LIST_HEAD(&tp->rx_done);
  1128. skb_queue_head_init(&tp->tx_queue);
  1129. skb_queue_head_init(&tp->rx_queue);
  1130. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1131. buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
  1132. if (!buf)
  1133. goto err1;
  1134. if (buf != rx_agg_align(buf)) {
  1135. kfree(buf);
  1136. buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
  1137. node);
  1138. if (!buf)
  1139. goto err1;
  1140. }
  1141. urb = usb_alloc_urb(0, GFP_KERNEL);
  1142. if (!urb) {
  1143. kfree(buf);
  1144. goto err1;
  1145. }
  1146. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1147. tp->rx_info[i].context = tp;
  1148. tp->rx_info[i].urb = urb;
  1149. tp->rx_info[i].buffer = buf;
  1150. tp->rx_info[i].head = rx_agg_align(buf);
  1151. }
  1152. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1153. buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
  1154. if (!buf)
  1155. goto err1;
  1156. if (buf != tx_agg_align(buf)) {
  1157. kfree(buf);
  1158. buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
  1159. node);
  1160. if (!buf)
  1161. goto err1;
  1162. }
  1163. urb = usb_alloc_urb(0, GFP_KERNEL);
  1164. if (!urb) {
  1165. kfree(buf);
  1166. goto err1;
  1167. }
  1168. INIT_LIST_HEAD(&tp->tx_info[i].list);
  1169. tp->tx_info[i].context = tp;
  1170. tp->tx_info[i].urb = urb;
  1171. tp->tx_info[i].buffer = buf;
  1172. tp->tx_info[i].head = tx_agg_align(buf);
  1173. list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
  1174. }
  1175. tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  1176. if (!tp->intr_urb)
  1177. goto err1;
  1178. tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  1179. if (!tp->intr_buff)
  1180. goto err1;
  1181. tp->intr_interval = (int)ep_intr->desc.bInterval;
  1182. usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
  1183. tp->intr_buff, INTBUFSIZE, intr_callback,
  1184. tp, tp->intr_interval);
  1185. return 0;
  1186. err1:
  1187. free_all_mem(tp);
  1188. return -ENOMEM;
  1189. }
  1190. static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
  1191. {
  1192. struct tx_agg *agg = NULL;
  1193. unsigned long flags;
  1194. if (list_empty(&tp->tx_free))
  1195. return NULL;
  1196. spin_lock_irqsave(&tp->tx_lock, flags);
  1197. if (!list_empty(&tp->tx_free)) {
  1198. struct list_head *cursor;
  1199. cursor = tp->tx_free.next;
  1200. list_del_init(cursor);
  1201. agg = list_entry(cursor, struct tx_agg, list);
  1202. }
  1203. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1204. return agg;
  1205. }
  1206. /* r8152_csum_workaround()
  1207. * The hw limites the value the transport offset. When the offset is out of the
  1208. * range, calculate the checksum by sw.
  1209. */
  1210. static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
  1211. struct sk_buff_head *list)
  1212. {
  1213. if (skb_shinfo(skb)->gso_size) {
  1214. netdev_features_t features = tp->netdev->features;
  1215. struct sk_buff_head seg_list;
  1216. struct sk_buff *segs, *nskb;
  1217. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  1218. segs = skb_gso_segment(skb, features);
  1219. if (IS_ERR(segs) || !segs)
  1220. goto drop;
  1221. __skb_queue_head_init(&seg_list);
  1222. do {
  1223. nskb = segs;
  1224. segs = segs->next;
  1225. nskb->next = NULL;
  1226. __skb_queue_tail(&seg_list, nskb);
  1227. } while (segs);
  1228. skb_queue_splice(&seg_list, list);
  1229. dev_kfree_skb(skb);
  1230. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1231. if (skb_checksum_help(skb) < 0)
  1232. goto drop;
  1233. __skb_queue_head(list, skb);
  1234. } else {
  1235. struct net_device_stats *stats;
  1236. drop:
  1237. stats = &tp->netdev->stats;
  1238. stats->tx_dropped++;
  1239. dev_kfree_skb(skb);
  1240. }
  1241. }
  1242. /* msdn_giant_send_check()
  1243. * According to the document of microsoft, the TCP Pseudo Header excludes the
  1244. * packet length for IPv6 TCP large packets.
  1245. */
  1246. static int msdn_giant_send_check(struct sk_buff *skb)
  1247. {
  1248. const struct ipv6hdr *ipv6h;
  1249. struct tcphdr *th;
  1250. int ret;
  1251. ret = skb_cow_head(skb, 0);
  1252. if (ret)
  1253. return ret;
  1254. ipv6h = ipv6_hdr(skb);
  1255. th = tcp_hdr(skb);
  1256. th->check = 0;
  1257. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  1258. return ret;
  1259. }
  1260. static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
  1261. {
  1262. if (skb_vlan_tag_present(skb)) {
  1263. u32 opts2;
  1264. opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
  1265. desc->opts2 |= cpu_to_le32(opts2);
  1266. }
  1267. }
  1268. static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
  1269. {
  1270. u32 opts2 = le32_to_cpu(desc->opts2);
  1271. if (opts2 & RX_VLAN_TAG)
  1272. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1273. swab16(opts2 & 0xffff));
  1274. }
  1275. static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
  1276. struct sk_buff *skb, u32 len, u32 transport_offset)
  1277. {
  1278. u32 mss = skb_shinfo(skb)->gso_size;
  1279. u32 opts1, opts2 = 0;
  1280. int ret = TX_CSUM_SUCCESS;
  1281. WARN_ON_ONCE(len > TX_LEN_MAX);
  1282. opts1 = len | TX_FS | TX_LS;
  1283. if (mss) {
  1284. if (transport_offset > GTTCPHO_MAX) {
  1285. netif_warn(tp, tx_err, tp->netdev,
  1286. "Invalid transport offset 0x%x for TSO\n",
  1287. transport_offset);
  1288. ret = TX_CSUM_TSO;
  1289. goto unavailable;
  1290. }
  1291. switch (vlan_get_protocol(skb)) {
  1292. case htons(ETH_P_IP):
  1293. opts1 |= GTSENDV4;
  1294. break;
  1295. case htons(ETH_P_IPV6):
  1296. if (msdn_giant_send_check(skb)) {
  1297. ret = TX_CSUM_TSO;
  1298. goto unavailable;
  1299. }
  1300. opts1 |= GTSENDV6;
  1301. break;
  1302. default:
  1303. WARN_ON_ONCE(1);
  1304. break;
  1305. }
  1306. opts1 |= transport_offset << GTTCPHO_SHIFT;
  1307. opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
  1308. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1309. u8 ip_protocol;
  1310. if (transport_offset > TCPHO_MAX) {
  1311. netif_warn(tp, tx_err, tp->netdev,
  1312. "Invalid transport offset 0x%x\n",
  1313. transport_offset);
  1314. ret = TX_CSUM_NONE;
  1315. goto unavailable;
  1316. }
  1317. switch (vlan_get_protocol(skb)) {
  1318. case htons(ETH_P_IP):
  1319. opts2 |= IPV4_CS;
  1320. ip_protocol = ip_hdr(skb)->protocol;
  1321. break;
  1322. case htons(ETH_P_IPV6):
  1323. opts2 |= IPV6_CS;
  1324. ip_protocol = ipv6_hdr(skb)->nexthdr;
  1325. break;
  1326. default:
  1327. ip_protocol = IPPROTO_RAW;
  1328. break;
  1329. }
  1330. if (ip_protocol == IPPROTO_TCP)
  1331. opts2 |= TCP_CS;
  1332. else if (ip_protocol == IPPROTO_UDP)
  1333. opts2 |= UDP_CS;
  1334. else
  1335. WARN_ON_ONCE(1);
  1336. opts2 |= transport_offset << TCPHO_SHIFT;
  1337. }
  1338. desc->opts2 = cpu_to_le32(opts2);
  1339. desc->opts1 = cpu_to_le32(opts1);
  1340. unavailable:
  1341. return ret;
  1342. }
  1343. static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
  1344. {
  1345. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1346. int remain, ret;
  1347. u8 *tx_data;
  1348. __skb_queue_head_init(&skb_head);
  1349. spin_lock(&tx_queue->lock);
  1350. skb_queue_splice_init(tx_queue, &skb_head);
  1351. spin_unlock(&tx_queue->lock);
  1352. tx_data = agg->head;
  1353. agg->skb_num = 0;
  1354. agg->skb_len = 0;
  1355. remain = agg_buf_sz;
  1356. while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
  1357. struct tx_desc *tx_desc;
  1358. struct sk_buff *skb;
  1359. unsigned int len;
  1360. u32 offset;
  1361. skb = __skb_dequeue(&skb_head);
  1362. if (!skb)
  1363. break;
  1364. len = skb->len + sizeof(*tx_desc);
  1365. if (len > remain) {
  1366. __skb_queue_head(&skb_head, skb);
  1367. break;
  1368. }
  1369. tx_data = tx_agg_align(tx_data);
  1370. tx_desc = (struct tx_desc *)tx_data;
  1371. offset = (u32)skb_transport_offset(skb);
  1372. if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
  1373. r8152_csum_workaround(tp, skb, &skb_head);
  1374. continue;
  1375. }
  1376. rtl_tx_vlan_tag(tx_desc, skb);
  1377. tx_data += sizeof(*tx_desc);
  1378. len = skb->len;
  1379. if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
  1380. struct net_device_stats *stats = &tp->netdev->stats;
  1381. stats->tx_dropped++;
  1382. dev_kfree_skb_any(skb);
  1383. tx_data -= sizeof(*tx_desc);
  1384. continue;
  1385. }
  1386. tx_data += len;
  1387. agg->skb_len += len;
  1388. agg->skb_num++;
  1389. dev_kfree_skb_any(skb);
  1390. remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
  1391. }
  1392. if (!skb_queue_empty(&skb_head)) {
  1393. spin_lock(&tx_queue->lock);
  1394. skb_queue_splice(&skb_head, tx_queue);
  1395. spin_unlock(&tx_queue->lock);
  1396. }
  1397. netif_tx_lock(tp->netdev);
  1398. if (netif_queue_stopped(tp->netdev) &&
  1399. skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
  1400. netif_wake_queue(tp->netdev);
  1401. netif_tx_unlock(tp->netdev);
  1402. ret = usb_autopm_get_interface_async(tp->intf);
  1403. if (ret < 0)
  1404. goto out_tx_fill;
  1405. usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  1406. agg->head, (int)(tx_data - (u8 *)agg->head),
  1407. (usb_complete_t)write_bulk_callback, agg);
  1408. ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
  1409. if (ret < 0)
  1410. usb_autopm_put_interface_async(tp->intf);
  1411. out_tx_fill:
  1412. return ret;
  1413. }
  1414. static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
  1415. {
  1416. u8 checksum = CHECKSUM_NONE;
  1417. u32 opts2, opts3;
  1418. if (!(tp->netdev->features & NETIF_F_RXCSUM))
  1419. goto return_result;
  1420. opts2 = le32_to_cpu(rx_desc->opts2);
  1421. opts3 = le32_to_cpu(rx_desc->opts3);
  1422. if (opts2 & RD_IPV4_CS) {
  1423. if (opts3 & IPF)
  1424. checksum = CHECKSUM_NONE;
  1425. else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
  1426. checksum = CHECKSUM_NONE;
  1427. else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
  1428. checksum = CHECKSUM_NONE;
  1429. else
  1430. checksum = CHECKSUM_UNNECESSARY;
  1431. } else if (opts2 & RD_IPV6_CS) {
  1432. if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
  1433. checksum = CHECKSUM_UNNECESSARY;
  1434. else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
  1435. checksum = CHECKSUM_UNNECESSARY;
  1436. }
  1437. return_result:
  1438. return checksum;
  1439. }
  1440. static int rx_bottom(struct r8152 *tp, int budget)
  1441. {
  1442. unsigned long flags;
  1443. struct list_head *cursor, *next, rx_queue;
  1444. int ret = 0, work_done = 0;
  1445. struct napi_struct *napi = &tp->napi;
  1446. if (!skb_queue_empty(&tp->rx_queue)) {
  1447. while (work_done < budget) {
  1448. struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
  1449. struct net_device *netdev = tp->netdev;
  1450. struct net_device_stats *stats = &netdev->stats;
  1451. unsigned int pkt_len;
  1452. if (!skb)
  1453. break;
  1454. pkt_len = skb->len;
  1455. napi_gro_receive(napi, skb);
  1456. work_done++;
  1457. stats->rx_packets++;
  1458. stats->rx_bytes += pkt_len;
  1459. }
  1460. }
  1461. if (list_empty(&tp->rx_done))
  1462. goto out1;
  1463. INIT_LIST_HEAD(&rx_queue);
  1464. spin_lock_irqsave(&tp->rx_lock, flags);
  1465. list_splice_init(&tp->rx_done, &rx_queue);
  1466. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1467. list_for_each_safe(cursor, next, &rx_queue) {
  1468. struct rx_desc *rx_desc;
  1469. struct rx_agg *agg;
  1470. int len_used = 0;
  1471. struct urb *urb;
  1472. u8 *rx_data;
  1473. list_del_init(cursor);
  1474. agg = list_entry(cursor, struct rx_agg, list);
  1475. urb = agg->urb;
  1476. if (urb->actual_length < ETH_ZLEN)
  1477. goto submit;
  1478. rx_desc = agg->head;
  1479. rx_data = agg->head;
  1480. len_used += sizeof(struct rx_desc);
  1481. while (urb->actual_length > len_used) {
  1482. struct net_device *netdev = tp->netdev;
  1483. struct net_device_stats *stats = &netdev->stats;
  1484. unsigned int pkt_len;
  1485. struct sk_buff *skb;
  1486. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  1487. if (pkt_len < ETH_ZLEN)
  1488. break;
  1489. len_used += pkt_len;
  1490. if (urb->actual_length < len_used)
  1491. break;
  1492. pkt_len -= CRC_SIZE;
  1493. rx_data += sizeof(struct rx_desc);
  1494. skb = napi_alloc_skb(napi, pkt_len);
  1495. if (!skb) {
  1496. stats->rx_dropped++;
  1497. goto find_next_rx;
  1498. }
  1499. skb->ip_summed = r8152_rx_csum(tp, rx_desc);
  1500. memcpy(skb->data, rx_data, pkt_len);
  1501. skb_put(skb, pkt_len);
  1502. skb->protocol = eth_type_trans(skb, netdev);
  1503. rtl_rx_vlan_tag(rx_desc, skb);
  1504. if (work_done < budget) {
  1505. napi_gro_receive(napi, skb);
  1506. work_done++;
  1507. stats->rx_packets++;
  1508. stats->rx_bytes += pkt_len;
  1509. } else {
  1510. __skb_queue_tail(&tp->rx_queue, skb);
  1511. }
  1512. find_next_rx:
  1513. rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
  1514. rx_desc = (struct rx_desc *)rx_data;
  1515. len_used = (int)(rx_data - (u8 *)agg->head);
  1516. len_used += sizeof(struct rx_desc);
  1517. }
  1518. submit:
  1519. if (!ret) {
  1520. ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  1521. } else {
  1522. urb->actual_length = 0;
  1523. list_add_tail(&agg->list, next);
  1524. }
  1525. }
  1526. if (!list_empty(&rx_queue)) {
  1527. spin_lock_irqsave(&tp->rx_lock, flags);
  1528. list_splice_tail(&rx_queue, &tp->rx_done);
  1529. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1530. }
  1531. out1:
  1532. return work_done;
  1533. }
  1534. static void tx_bottom(struct r8152 *tp)
  1535. {
  1536. int res;
  1537. do {
  1538. struct tx_agg *agg;
  1539. if (skb_queue_empty(&tp->tx_queue))
  1540. break;
  1541. agg = r8152_get_tx_agg(tp);
  1542. if (!agg)
  1543. break;
  1544. res = r8152_tx_agg_fill(tp, agg);
  1545. if (res) {
  1546. struct net_device *netdev = tp->netdev;
  1547. if (res == -ENODEV) {
  1548. set_bit(RTL8152_UNPLUG, &tp->flags);
  1549. netif_device_detach(netdev);
  1550. } else {
  1551. struct net_device_stats *stats = &netdev->stats;
  1552. unsigned long flags;
  1553. netif_warn(tp, tx_err, netdev,
  1554. "failed tx_urb %d\n", res);
  1555. stats->tx_dropped += agg->skb_num;
  1556. spin_lock_irqsave(&tp->tx_lock, flags);
  1557. list_add_tail(&agg->list, &tp->tx_free);
  1558. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1559. }
  1560. }
  1561. } while (res == 0);
  1562. }
  1563. static void bottom_half(struct r8152 *tp)
  1564. {
  1565. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1566. return;
  1567. if (!test_bit(WORK_ENABLE, &tp->flags))
  1568. return;
  1569. /* When link down, the driver would cancel all bulks. */
  1570. /* This avoid the re-submitting bulk */
  1571. if (!netif_carrier_ok(tp->netdev))
  1572. return;
  1573. clear_bit(SCHEDULE_NAPI, &tp->flags);
  1574. tx_bottom(tp);
  1575. }
  1576. static int r8152_poll(struct napi_struct *napi, int budget)
  1577. {
  1578. struct r8152 *tp = container_of(napi, struct r8152, napi);
  1579. int work_done;
  1580. work_done = rx_bottom(tp, budget);
  1581. bottom_half(tp);
  1582. if (work_done < budget) {
  1583. napi_complete(napi);
  1584. if (!list_empty(&tp->rx_done))
  1585. napi_schedule(napi);
  1586. else if (!skb_queue_empty(&tp->tx_queue) &&
  1587. !list_empty(&tp->tx_free))
  1588. napi_schedule(napi);
  1589. }
  1590. return work_done;
  1591. }
  1592. static
  1593. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
  1594. {
  1595. int ret;
  1596. /* The rx would be stopped, so skip submitting */
  1597. if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
  1598. !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
  1599. return 0;
  1600. usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  1601. agg->head, agg_buf_sz,
  1602. (usb_complete_t)read_bulk_callback, agg);
  1603. ret = usb_submit_urb(agg->urb, mem_flags);
  1604. if (ret == -ENODEV) {
  1605. set_bit(RTL8152_UNPLUG, &tp->flags);
  1606. netif_device_detach(tp->netdev);
  1607. } else if (ret) {
  1608. struct urb *urb = agg->urb;
  1609. unsigned long flags;
  1610. urb->actual_length = 0;
  1611. spin_lock_irqsave(&tp->rx_lock, flags);
  1612. list_add_tail(&agg->list, &tp->rx_done);
  1613. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1614. netif_err(tp, rx_err, tp->netdev,
  1615. "Couldn't submit rx[%p], ret = %d\n", agg, ret);
  1616. napi_schedule(&tp->napi);
  1617. }
  1618. return ret;
  1619. }
  1620. static void rtl_drop_queued_tx(struct r8152 *tp)
  1621. {
  1622. struct net_device_stats *stats = &tp->netdev->stats;
  1623. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1624. struct sk_buff *skb;
  1625. if (skb_queue_empty(tx_queue))
  1626. return;
  1627. __skb_queue_head_init(&skb_head);
  1628. spin_lock_bh(&tx_queue->lock);
  1629. skb_queue_splice_init(tx_queue, &skb_head);
  1630. spin_unlock_bh(&tx_queue->lock);
  1631. while ((skb = __skb_dequeue(&skb_head))) {
  1632. dev_kfree_skb(skb);
  1633. stats->tx_dropped++;
  1634. }
  1635. }
  1636. static void rtl8152_tx_timeout(struct net_device *netdev)
  1637. {
  1638. struct r8152 *tp = netdev_priv(netdev);
  1639. netif_warn(tp, tx_err, netdev, "Tx timeout\n");
  1640. usb_queue_reset_device(tp->intf);
  1641. }
  1642. static void rtl8152_set_rx_mode(struct net_device *netdev)
  1643. {
  1644. struct r8152 *tp = netdev_priv(netdev);
  1645. if (netif_carrier_ok(netdev)) {
  1646. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1647. schedule_delayed_work(&tp->schedule, 0);
  1648. }
  1649. }
  1650. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  1651. {
  1652. struct r8152 *tp = netdev_priv(netdev);
  1653. u32 mc_filter[2]; /* Multicast hash filter */
  1654. __le32 tmp[2];
  1655. u32 ocp_data;
  1656. netif_stop_queue(netdev);
  1657. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1658. ocp_data &= ~RCR_ACPT_ALL;
  1659. ocp_data |= RCR_AB | RCR_APM;
  1660. if (netdev->flags & IFF_PROMISC) {
  1661. /* Unconditionally log net taps. */
  1662. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  1663. ocp_data |= RCR_AM | RCR_AAP;
  1664. mc_filter[1] = 0xffffffff;
  1665. mc_filter[0] = 0xffffffff;
  1666. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  1667. (netdev->flags & IFF_ALLMULTI)) {
  1668. /* Too many to filter perfectly -- accept all multicasts. */
  1669. ocp_data |= RCR_AM;
  1670. mc_filter[1] = 0xffffffff;
  1671. mc_filter[0] = 0xffffffff;
  1672. } else {
  1673. struct netdev_hw_addr *ha;
  1674. mc_filter[1] = 0;
  1675. mc_filter[0] = 0;
  1676. netdev_for_each_mc_addr(ha, netdev) {
  1677. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1678. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1679. ocp_data |= RCR_AM;
  1680. }
  1681. }
  1682. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  1683. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  1684. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  1685. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1686. netif_wake_queue(netdev);
  1687. }
  1688. static netdev_features_t
  1689. rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
  1690. netdev_features_t features)
  1691. {
  1692. u32 mss = skb_shinfo(skb)->gso_size;
  1693. int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
  1694. int offset = skb_transport_offset(skb);
  1695. if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
  1696. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1697. else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
  1698. features &= ~NETIF_F_GSO_MASK;
  1699. return features;
  1700. }
  1701. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  1702. struct net_device *netdev)
  1703. {
  1704. struct r8152 *tp = netdev_priv(netdev);
  1705. skb_tx_timestamp(skb);
  1706. skb_queue_tail(&tp->tx_queue, skb);
  1707. if (!list_empty(&tp->tx_free)) {
  1708. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  1709. set_bit(SCHEDULE_NAPI, &tp->flags);
  1710. schedule_delayed_work(&tp->schedule, 0);
  1711. } else {
  1712. usb_mark_last_busy(tp->udev);
  1713. napi_schedule(&tp->napi);
  1714. }
  1715. } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
  1716. netif_stop_queue(netdev);
  1717. }
  1718. return NETDEV_TX_OK;
  1719. }
  1720. static void r8152b_reset_packet_filter(struct r8152 *tp)
  1721. {
  1722. u32 ocp_data;
  1723. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  1724. ocp_data &= ~FMC_FCR_MCU_EN;
  1725. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1726. ocp_data |= FMC_FCR_MCU_EN;
  1727. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1728. }
  1729. static void rtl8152_nic_reset(struct r8152 *tp)
  1730. {
  1731. int i;
  1732. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  1733. for (i = 0; i < 1000; i++) {
  1734. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  1735. break;
  1736. usleep_range(100, 400);
  1737. }
  1738. }
  1739. static void set_tx_qlen(struct r8152 *tp)
  1740. {
  1741. struct net_device *netdev = tp->netdev;
  1742. tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
  1743. sizeof(struct tx_desc));
  1744. }
  1745. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  1746. {
  1747. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  1748. }
  1749. static void rtl_set_eee_plus(struct r8152 *tp)
  1750. {
  1751. u32 ocp_data;
  1752. u8 speed;
  1753. speed = rtl8152_get_speed(tp);
  1754. if (speed & _10bps) {
  1755. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1756. ocp_data |= EEEP_CR_EEEP_TX;
  1757. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1758. } else {
  1759. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1760. ocp_data &= ~EEEP_CR_EEEP_TX;
  1761. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1762. }
  1763. }
  1764. static void rxdy_gated_en(struct r8152 *tp, bool enable)
  1765. {
  1766. u32 ocp_data;
  1767. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1768. if (enable)
  1769. ocp_data |= RXDY_GATED_EN;
  1770. else
  1771. ocp_data &= ~RXDY_GATED_EN;
  1772. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1773. }
  1774. static int rtl_start_rx(struct r8152 *tp)
  1775. {
  1776. int i, ret = 0;
  1777. INIT_LIST_HEAD(&tp->rx_done);
  1778. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1779. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1780. ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
  1781. if (ret)
  1782. break;
  1783. }
  1784. if (ret && ++i < RTL8152_MAX_RX) {
  1785. struct list_head rx_queue;
  1786. unsigned long flags;
  1787. INIT_LIST_HEAD(&rx_queue);
  1788. do {
  1789. struct rx_agg *agg = &tp->rx_info[i++];
  1790. struct urb *urb = agg->urb;
  1791. urb->actual_length = 0;
  1792. list_add_tail(&agg->list, &rx_queue);
  1793. } while (i < RTL8152_MAX_RX);
  1794. spin_lock_irqsave(&tp->rx_lock, flags);
  1795. list_splice_tail(&rx_queue, &tp->rx_done);
  1796. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1797. }
  1798. return ret;
  1799. }
  1800. static int rtl_stop_rx(struct r8152 *tp)
  1801. {
  1802. int i;
  1803. for (i = 0; i < RTL8152_MAX_RX; i++)
  1804. usb_kill_urb(tp->rx_info[i].urb);
  1805. while (!skb_queue_empty(&tp->rx_queue))
  1806. dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
  1807. return 0;
  1808. }
  1809. static int rtl_enable(struct r8152 *tp)
  1810. {
  1811. u32 ocp_data;
  1812. r8152b_reset_packet_filter(tp);
  1813. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  1814. ocp_data |= CR_RE | CR_TE;
  1815. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  1816. rxdy_gated_en(tp, false);
  1817. return 0;
  1818. }
  1819. static int rtl8152_enable(struct r8152 *tp)
  1820. {
  1821. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1822. return -ENODEV;
  1823. set_tx_qlen(tp);
  1824. rtl_set_eee_plus(tp);
  1825. return rtl_enable(tp);
  1826. }
  1827. static void r8153_set_rx_early_timeout(struct r8152 *tp)
  1828. {
  1829. u32 ocp_data = tp->coalesce / 8;
  1830. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data);
  1831. }
  1832. static void r8153_set_rx_early_size(struct r8152 *tp)
  1833. {
  1834. u32 ocp_data = (agg_buf_sz - rx_reserved_size(tp->netdev->mtu)) / 4;
  1835. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data);
  1836. }
  1837. static int rtl8153_enable(struct r8152 *tp)
  1838. {
  1839. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1840. return -ENODEV;
  1841. usb_disable_lpm(tp->udev);
  1842. set_tx_qlen(tp);
  1843. rtl_set_eee_plus(tp);
  1844. r8153_set_rx_early_timeout(tp);
  1845. r8153_set_rx_early_size(tp);
  1846. return rtl_enable(tp);
  1847. }
  1848. static void rtl_disable(struct r8152 *tp)
  1849. {
  1850. u32 ocp_data;
  1851. int i;
  1852. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  1853. rtl_drop_queued_tx(tp);
  1854. return;
  1855. }
  1856. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1857. ocp_data &= ~RCR_ACPT_ALL;
  1858. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1859. rtl_drop_queued_tx(tp);
  1860. for (i = 0; i < RTL8152_MAX_TX; i++)
  1861. usb_kill_urb(tp->tx_info[i].urb);
  1862. rxdy_gated_en(tp, true);
  1863. for (i = 0; i < 1000; i++) {
  1864. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1865. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  1866. break;
  1867. usleep_range(1000, 2000);
  1868. }
  1869. for (i = 0; i < 1000; i++) {
  1870. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  1871. break;
  1872. usleep_range(1000, 2000);
  1873. }
  1874. rtl_stop_rx(tp);
  1875. rtl8152_nic_reset(tp);
  1876. }
  1877. static void r8152_power_cut_en(struct r8152 *tp, bool enable)
  1878. {
  1879. u32 ocp_data;
  1880. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1881. if (enable)
  1882. ocp_data |= POWER_CUT;
  1883. else
  1884. ocp_data &= ~POWER_CUT;
  1885. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1886. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1887. ocp_data &= ~RESUME_INDICATE;
  1888. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1889. }
  1890. static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
  1891. {
  1892. u32 ocp_data;
  1893. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1894. if (enable)
  1895. ocp_data |= CPCR_RX_VLAN;
  1896. else
  1897. ocp_data &= ~CPCR_RX_VLAN;
  1898. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1899. }
  1900. static int rtl8152_set_features(struct net_device *dev,
  1901. netdev_features_t features)
  1902. {
  1903. netdev_features_t changed = features ^ dev->features;
  1904. struct r8152 *tp = netdev_priv(dev);
  1905. int ret;
  1906. ret = usb_autopm_get_interface(tp->intf);
  1907. if (ret < 0)
  1908. goto out;
  1909. mutex_lock(&tp->control);
  1910. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  1911. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1912. rtl_rx_vlan_en(tp, true);
  1913. else
  1914. rtl_rx_vlan_en(tp, false);
  1915. }
  1916. mutex_unlock(&tp->control);
  1917. usb_autopm_put_interface(tp->intf);
  1918. out:
  1919. return ret;
  1920. }
  1921. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1922. static u32 __rtl_get_wol(struct r8152 *tp)
  1923. {
  1924. u32 ocp_data;
  1925. u32 wolopts = 0;
  1926. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1927. if (ocp_data & LINK_ON_WAKE_EN)
  1928. wolopts |= WAKE_PHY;
  1929. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1930. if (ocp_data & UWF_EN)
  1931. wolopts |= WAKE_UCAST;
  1932. if (ocp_data & BWF_EN)
  1933. wolopts |= WAKE_BCAST;
  1934. if (ocp_data & MWF_EN)
  1935. wolopts |= WAKE_MCAST;
  1936. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1937. if (ocp_data & MAGIC_EN)
  1938. wolopts |= WAKE_MAGIC;
  1939. return wolopts;
  1940. }
  1941. static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
  1942. {
  1943. u32 ocp_data;
  1944. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1945. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1946. ocp_data &= ~LINK_ON_WAKE_EN;
  1947. if (wolopts & WAKE_PHY)
  1948. ocp_data |= LINK_ON_WAKE_EN;
  1949. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1950. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1951. ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
  1952. if (wolopts & WAKE_UCAST)
  1953. ocp_data |= UWF_EN;
  1954. if (wolopts & WAKE_BCAST)
  1955. ocp_data |= BWF_EN;
  1956. if (wolopts & WAKE_MCAST)
  1957. ocp_data |= MWF_EN;
  1958. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
  1959. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1960. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1961. ocp_data &= ~MAGIC_EN;
  1962. if (wolopts & WAKE_MAGIC)
  1963. ocp_data |= MAGIC_EN;
  1964. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  1965. if (wolopts & WAKE_ANY)
  1966. device_set_wakeup_enable(&tp->udev->dev, true);
  1967. else
  1968. device_set_wakeup_enable(&tp->udev->dev, false);
  1969. }
  1970. static void r8153_u1u2en(struct r8152 *tp, bool enable)
  1971. {
  1972. u8 u1u2[8];
  1973. if (enable)
  1974. memset(u1u2, 0xff, sizeof(u1u2));
  1975. else
  1976. memset(u1u2, 0x00, sizeof(u1u2));
  1977. usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
  1978. }
  1979. static void r8153_u2p3en(struct r8152 *tp, bool enable)
  1980. {
  1981. u32 ocp_data;
  1982. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  1983. if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04)
  1984. ocp_data |= U2P3_ENABLE;
  1985. else
  1986. ocp_data &= ~U2P3_ENABLE;
  1987. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  1988. }
  1989. static void r8153_power_cut_en(struct r8152 *tp, bool enable)
  1990. {
  1991. u32 ocp_data;
  1992. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  1993. if (enable)
  1994. ocp_data |= PWR_EN | PHASE2_EN;
  1995. else
  1996. ocp_data &= ~(PWR_EN | PHASE2_EN);
  1997. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  1998. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  1999. ocp_data &= ~PCUT_STATUS;
  2000. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  2001. }
  2002. static bool rtl_can_wakeup(struct r8152 *tp)
  2003. {
  2004. struct usb_device *udev = tp->udev;
  2005. return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
  2006. }
  2007. static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
  2008. {
  2009. if (enable) {
  2010. u32 ocp_data;
  2011. __rtl_set_wol(tp, WAKE_ANY);
  2012. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  2013. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  2014. ocp_data |= LINK_OFF_WAKE_EN;
  2015. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  2016. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  2017. } else {
  2018. u32 ocp_data;
  2019. __rtl_set_wol(tp, tp->saved_wolopts);
  2020. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  2021. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  2022. ocp_data &= ~LINK_OFF_WAKE_EN;
  2023. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  2024. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  2025. }
  2026. }
  2027. static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
  2028. {
  2029. rtl_runtime_suspend_enable(tp, enable);
  2030. if (enable) {
  2031. r8153_u1u2en(tp, false);
  2032. r8153_u2p3en(tp, false);
  2033. } else {
  2034. r8153_u2p3en(tp, true);
  2035. r8153_u1u2en(tp, true);
  2036. }
  2037. }
  2038. static void r8153_teredo_off(struct r8152 *tp)
  2039. {
  2040. u32 ocp_data;
  2041. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  2042. ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
  2043. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  2044. ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
  2045. ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
  2046. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
  2047. }
  2048. static void rtl_reset_bmu(struct r8152 *tp)
  2049. {
  2050. u32 ocp_data;
  2051. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
  2052. ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
  2053. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  2054. ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
  2055. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  2056. }
  2057. static void r8152_aldps_en(struct r8152 *tp, bool enable)
  2058. {
  2059. if (enable) {
  2060. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  2061. LINKENA | DIS_SDSAVE);
  2062. } else {
  2063. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
  2064. DIS_SDSAVE);
  2065. msleep(20);
  2066. }
  2067. }
  2068. static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
  2069. {
  2070. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
  2071. ocp_reg_write(tp, OCP_EEE_DATA, reg);
  2072. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
  2073. }
  2074. static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
  2075. {
  2076. u16 data;
  2077. r8152_mmd_indirect(tp, dev, reg);
  2078. data = ocp_reg_read(tp, OCP_EEE_DATA);
  2079. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  2080. return data;
  2081. }
  2082. static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
  2083. {
  2084. r8152_mmd_indirect(tp, dev, reg);
  2085. ocp_reg_write(tp, OCP_EEE_DATA, data);
  2086. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  2087. }
  2088. static void r8152_eee_en(struct r8152 *tp, bool enable)
  2089. {
  2090. u16 config1, config2, config3;
  2091. u32 ocp_data;
  2092. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2093. config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
  2094. config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
  2095. config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
  2096. if (enable) {
  2097. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2098. config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
  2099. config1 |= sd_rise_time(1);
  2100. config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
  2101. config3 |= fast_snr(42);
  2102. } else {
  2103. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  2104. config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
  2105. RX_QUIET_EN);
  2106. config1 |= sd_rise_time(7);
  2107. config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
  2108. config3 |= fast_snr(511);
  2109. }
  2110. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2111. ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
  2112. ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
  2113. ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
  2114. }
  2115. static void r8152b_enable_eee(struct r8152 *tp)
  2116. {
  2117. r8152_eee_en(tp, true);
  2118. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
  2119. }
  2120. static void r8152b_enable_fc(struct r8152 *tp)
  2121. {
  2122. u16 anar;
  2123. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2124. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  2125. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2126. }
  2127. static void rtl8152_disable(struct r8152 *tp)
  2128. {
  2129. r8152_aldps_en(tp, false);
  2130. rtl_disable(tp);
  2131. r8152_aldps_en(tp, true);
  2132. }
  2133. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  2134. {
  2135. r8152b_enable_eee(tp);
  2136. r8152_aldps_en(tp, true);
  2137. r8152b_enable_fc(tp);
  2138. set_bit(PHY_RESET, &tp->flags);
  2139. }
  2140. static void r8152b_exit_oob(struct r8152 *tp)
  2141. {
  2142. u32 ocp_data;
  2143. int i;
  2144. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2145. ocp_data &= ~RCR_ACPT_ALL;
  2146. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2147. rxdy_gated_en(tp, true);
  2148. r8153_teredo_off(tp);
  2149. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  2150. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  2151. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2152. ocp_data &= ~NOW_IS_OOB;
  2153. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2154. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2155. ocp_data &= ~MCU_BORW_EN;
  2156. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2157. for (i = 0; i < 1000; i++) {
  2158. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2159. if (ocp_data & LINK_LIST_READY)
  2160. break;
  2161. usleep_range(1000, 2000);
  2162. }
  2163. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2164. ocp_data |= RE_INIT_LL;
  2165. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2166. for (i = 0; i < 1000; i++) {
  2167. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2168. if (ocp_data & LINK_LIST_READY)
  2169. break;
  2170. usleep_range(1000, 2000);
  2171. }
  2172. rtl8152_nic_reset(tp);
  2173. /* rx share fifo credit full threshold */
  2174. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  2175. if (tp->udev->speed == USB_SPEED_FULL ||
  2176. tp->udev->speed == USB_SPEED_LOW) {
  2177. /* rx share fifo credit near full threshold */
  2178. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  2179. RXFIFO_THR2_FULL);
  2180. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  2181. RXFIFO_THR3_FULL);
  2182. } else {
  2183. /* rx share fifo credit near full threshold */
  2184. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  2185. RXFIFO_THR2_HIGH);
  2186. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  2187. RXFIFO_THR3_HIGH);
  2188. }
  2189. /* TX share fifo free credit full threshold */
  2190. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  2191. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  2192. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
  2193. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  2194. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  2195. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  2196. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2197. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  2198. ocp_data |= TCR0_AUTO_FIFO;
  2199. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  2200. }
  2201. static void r8152b_enter_oob(struct r8152 *tp)
  2202. {
  2203. u32 ocp_data;
  2204. int i;
  2205. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2206. ocp_data &= ~NOW_IS_OOB;
  2207. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2208. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  2209. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  2210. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  2211. rtl_disable(tp);
  2212. for (i = 0; i < 1000; i++) {
  2213. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2214. if (ocp_data & LINK_LIST_READY)
  2215. break;
  2216. usleep_range(1000, 2000);
  2217. }
  2218. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2219. ocp_data |= RE_INIT_LL;
  2220. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2221. for (i = 0; i < 1000; i++) {
  2222. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2223. if (ocp_data & LINK_LIST_READY)
  2224. break;
  2225. usleep_range(1000, 2000);
  2226. }
  2227. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2228. rtl_rx_vlan_en(tp, true);
  2229. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  2230. ocp_data |= ALDPS_PROXY_MODE;
  2231. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  2232. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2233. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  2234. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2235. rxdy_gated_en(tp, false);
  2236. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2237. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  2238. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2239. }
  2240. static void r8153_aldps_en(struct r8152 *tp, bool enable)
  2241. {
  2242. u16 data;
  2243. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2244. if (enable) {
  2245. data |= EN_ALDPS;
  2246. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2247. } else {
  2248. data &= ~EN_ALDPS;
  2249. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2250. msleep(20);
  2251. }
  2252. }
  2253. static void r8153_eee_en(struct r8152 *tp, bool enable)
  2254. {
  2255. u32 ocp_data;
  2256. u16 config;
  2257. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2258. config = ocp_reg_read(tp, OCP_EEE_CFG);
  2259. if (enable) {
  2260. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2261. config |= EEE10_EN;
  2262. } else {
  2263. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  2264. config &= ~EEE10_EN;
  2265. }
  2266. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2267. ocp_reg_write(tp, OCP_EEE_CFG, config);
  2268. }
  2269. static void r8153_hw_phy_cfg(struct r8152 *tp)
  2270. {
  2271. u32 ocp_data;
  2272. u16 data;
  2273. /* disable ALDPS before updating the PHY parameters */
  2274. r8153_aldps_en(tp, false);
  2275. /* disable EEE before updating the PHY parameters */
  2276. r8153_eee_en(tp, false);
  2277. ocp_reg_write(tp, OCP_EEE_ADV, 0);
  2278. if (tp->version == RTL_VER_03) {
  2279. data = ocp_reg_read(tp, OCP_EEE_CFG);
  2280. data &= ~CTAP_SHORT_EN;
  2281. ocp_reg_write(tp, OCP_EEE_CFG, data);
  2282. }
  2283. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2284. data |= EEE_CLKDIV_EN;
  2285. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2286. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  2287. data |= EN_10M_BGOFF;
  2288. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  2289. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2290. data |= EN_10M_PLLOFF;
  2291. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2292. sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
  2293. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  2294. ocp_data |= PFM_PWM_SWITCH;
  2295. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  2296. /* Enable LPF corner auto tune */
  2297. sram_write(tp, SRAM_LPF_CFG, 0xf70f);
  2298. /* Adjust 10M Amplitude */
  2299. sram_write(tp, SRAM_10M_AMP1, 0x00af);
  2300. sram_write(tp, SRAM_10M_AMP2, 0x0208);
  2301. r8153_eee_en(tp, true);
  2302. ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
  2303. r8153_aldps_en(tp, true);
  2304. r8152b_enable_fc(tp);
  2305. set_bit(PHY_RESET, &tp->flags);
  2306. }
  2307. static void r8153_first_init(struct r8152 *tp)
  2308. {
  2309. u32 ocp_data;
  2310. int i;
  2311. rxdy_gated_en(tp, true);
  2312. r8153_teredo_off(tp);
  2313. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2314. ocp_data &= ~RCR_ACPT_ALL;
  2315. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2316. rtl8152_nic_reset(tp);
  2317. rtl_reset_bmu(tp);
  2318. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2319. ocp_data &= ~NOW_IS_OOB;
  2320. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2321. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2322. ocp_data &= ~MCU_BORW_EN;
  2323. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2324. for (i = 0; i < 1000; i++) {
  2325. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2326. if (ocp_data & LINK_LIST_READY)
  2327. break;
  2328. usleep_range(1000, 2000);
  2329. }
  2330. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2331. ocp_data |= RE_INIT_LL;
  2332. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2333. for (i = 0; i < 1000; i++) {
  2334. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2335. if (ocp_data & LINK_LIST_READY)
  2336. break;
  2337. usleep_range(1000, 2000);
  2338. }
  2339. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  2340. ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + CRC_SIZE;
  2341. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
  2342. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
  2343. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  2344. ocp_data |= TCR0_AUTO_FIFO;
  2345. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  2346. rtl8152_nic_reset(tp);
  2347. /* rx share fifo credit full threshold */
  2348. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  2349. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  2350. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  2351. /* TX share fifo free credit full threshold */
  2352. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  2353. /* rx aggregation */
  2354. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2355. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  2356. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2357. }
  2358. static void r8153_enter_oob(struct r8152 *tp)
  2359. {
  2360. u32 ocp_data;
  2361. int i;
  2362. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2363. ocp_data &= ~NOW_IS_OOB;
  2364. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2365. rtl_disable(tp);
  2366. rtl_reset_bmu(tp);
  2367. for (i = 0; i < 1000; i++) {
  2368. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2369. if (ocp_data & LINK_LIST_READY)
  2370. break;
  2371. usleep_range(1000, 2000);
  2372. }
  2373. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2374. ocp_data |= RE_INIT_LL;
  2375. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2376. for (i = 0; i < 1000; i++) {
  2377. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2378. if (ocp_data & LINK_LIST_READY)
  2379. break;
  2380. usleep_range(1000, 2000);
  2381. }
  2382. ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + CRC_SIZE;
  2383. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
  2384. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  2385. ocp_data &= ~TEREDO_WAKE_MASK;
  2386. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  2387. rtl_rx_vlan_en(tp, true);
  2388. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  2389. ocp_data |= ALDPS_PROXY_MODE;
  2390. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  2391. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2392. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  2393. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2394. rxdy_gated_en(tp, false);
  2395. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2396. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  2397. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2398. }
  2399. static void rtl8153_disable(struct r8152 *tp)
  2400. {
  2401. r8153_aldps_en(tp, false);
  2402. rtl_disable(tp);
  2403. rtl_reset_bmu(tp);
  2404. r8153_aldps_en(tp, true);
  2405. usb_enable_lpm(tp->udev);
  2406. }
  2407. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  2408. {
  2409. u16 bmcr, anar, gbcr;
  2410. int ret = 0;
  2411. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2412. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  2413. ADVERTISE_100HALF | ADVERTISE_100FULL);
  2414. if (tp->mii.supports_gmii) {
  2415. gbcr = r8152_mdio_read(tp, MII_CTRL1000);
  2416. gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  2417. } else {
  2418. gbcr = 0;
  2419. }
  2420. if (autoneg == AUTONEG_DISABLE) {
  2421. if (speed == SPEED_10) {
  2422. bmcr = 0;
  2423. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2424. } else if (speed == SPEED_100) {
  2425. bmcr = BMCR_SPEED100;
  2426. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2427. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2428. bmcr = BMCR_SPEED1000;
  2429. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2430. } else {
  2431. ret = -EINVAL;
  2432. goto out;
  2433. }
  2434. if (duplex == DUPLEX_FULL)
  2435. bmcr |= BMCR_FULLDPLX;
  2436. } else {
  2437. if (speed == SPEED_10) {
  2438. if (duplex == DUPLEX_FULL)
  2439. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2440. else
  2441. anar |= ADVERTISE_10HALF;
  2442. } else if (speed == SPEED_100) {
  2443. if (duplex == DUPLEX_FULL) {
  2444. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2445. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2446. } else {
  2447. anar |= ADVERTISE_10HALF;
  2448. anar |= ADVERTISE_100HALF;
  2449. }
  2450. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2451. if (duplex == DUPLEX_FULL) {
  2452. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2453. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2454. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2455. } else {
  2456. anar |= ADVERTISE_10HALF;
  2457. anar |= ADVERTISE_100HALF;
  2458. gbcr |= ADVERTISE_1000HALF;
  2459. }
  2460. } else {
  2461. ret = -EINVAL;
  2462. goto out;
  2463. }
  2464. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  2465. }
  2466. if (test_and_clear_bit(PHY_RESET, &tp->flags))
  2467. bmcr |= BMCR_RESET;
  2468. if (tp->mii.supports_gmii)
  2469. r8152_mdio_write(tp, MII_CTRL1000, gbcr);
  2470. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2471. r8152_mdio_write(tp, MII_BMCR, bmcr);
  2472. if (bmcr & BMCR_RESET) {
  2473. int i;
  2474. for (i = 0; i < 50; i++) {
  2475. msleep(20);
  2476. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  2477. break;
  2478. }
  2479. }
  2480. out:
  2481. return ret;
  2482. }
  2483. static void rtl8152_up(struct r8152 *tp)
  2484. {
  2485. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2486. return;
  2487. r8152_aldps_en(tp, false);
  2488. r8152b_exit_oob(tp);
  2489. r8152_aldps_en(tp, true);
  2490. }
  2491. static void rtl8152_down(struct r8152 *tp)
  2492. {
  2493. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2494. rtl_drop_queued_tx(tp);
  2495. return;
  2496. }
  2497. r8152_power_cut_en(tp, false);
  2498. r8152_aldps_en(tp, false);
  2499. r8152b_enter_oob(tp);
  2500. r8152_aldps_en(tp, true);
  2501. }
  2502. static void rtl8153_up(struct r8152 *tp)
  2503. {
  2504. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2505. return;
  2506. r8153_u1u2en(tp, false);
  2507. r8153_aldps_en(tp, false);
  2508. r8153_first_init(tp);
  2509. r8153_aldps_en(tp, true);
  2510. r8153_u2p3en(tp, true);
  2511. r8153_u1u2en(tp, true);
  2512. usb_enable_lpm(tp->udev);
  2513. }
  2514. static void rtl8153_down(struct r8152 *tp)
  2515. {
  2516. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2517. rtl_drop_queued_tx(tp);
  2518. return;
  2519. }
  2520. r8153_u1u2en(tp, false);
  2521. r8153_u2p3en(tp, false);
  2522. r8153_power_cut_en(tp, false);
  2523. r8153_aldps_en(tp, false);
  2524. r8153_enter_oob(tp);
  2525. r8153_aldps_en(tp, true);
  2526. }
  2527. static bool rtl8152_in_nway(struct r8152 *tp)
  2528. {
  2529. u16 nway_state;
  2530. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
  2531. tp->ocp_base = 0x2000;
  2532. ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
  2533. nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
  2534. /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
  2535. if (nway_state & 0xc000)
  2536. return false;
  2537. else
  2538. return true;
  2539. }
  2540. static bool rtl8153_in_nway(struct r8152 *tp)
  2541. {
  2542. u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
  2543. if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
  2544. return false;
  2545. else
  2546. return true;
  2547. }
  2548. static void set_carrier(struct r8152 *tp)
  2549. {
  2550. struct net_device *netdev = tp->netdev;
  2551. struct napi_struct *napi = &tp->napi;
  2552. u8 speed;
  2553. speed = rtl8152_get_speed(tp);
  2554. if (speed & LINK_STATUS) {
  2555. if (!netif_carrier_ok(netdev)) {
  2556. tp->rtl_ops.enable(tp);
  2557. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  2558. netif_stop_queue(netdev);
  2559. napi_disable(napi);
  2560. netif_carrier_on(netdev);
  2561. rtl_start_rx(tp);
  2562. napi_enable(&tp->napi);
  2563. netif_wake_queue(netdev);
  2564. netif_info(tp, link, netdev, "carrier on\n");
  2565. } else if (netif_queue_stopped(netdev) &&
  2566. skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
  2567. netif_wake_queue(netdev);
  2568. }
  2569. } else {
  2570. if (netif_carrier_ok(netdev)) {
  2571. netif_carrier_off(netdev);
  2572. napi_disable(napi);
  2573. tp->rtl_ops.disable(tp);
  2574. napi_enable(napi);
  2575. netif_info(tp, link, netdev, "carrier off\n");
  2576. }
  2577. }
  2578. }
  2579. static void rtl_work_func_t(struct work_struct *work)
  2580. {
  2581. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  2582. /* If the device is unplugged or !netif_running(), the workqueue
  2583. * doesn't need to wake the device, and could return directly.
  2584. */
  2585. if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
  2586. return;
  2587. if (usb_autopm_get_interface(tp->intf) < 0)
  2588. return;
  2589. if (!test_bit(WORK_ENABLE, &tp->flags))
  2590. goto out1;
  2591. if (!mutex_trylock(&tp->control)) {
  2592. schedule_delayed_work(&tp->schedule, 0);
  2593. goto out1;
  2594. }
  2595. if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
  2596. set_carrier(tp);
  2597. if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
  2598. _rtl8152_set_rx_mode(tp->netdev);
  2599. /* don't schedule napi before linking */
  2600. if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
  2601. netif_carrier_ok(tp->netdev))
  2602. napi_schedule(&tp->napi);
  2603. mutex_unlock(&tp->control);
  2604. out1:
  2605. usb_autopm_put_interface(tp->intf);
  2606. }
  2607. static void rtl_hw_phy_work_func_t(struct work_struct *work)
  2608. {
  2609. struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
  2610. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2611. return;
  2612. if (usb_autopm_get_interface(tp->intf) < 0)
  2613. return;
  2614. mutex_lock(&tp->control);
  2615. tp->rtl_ops.hw_phy_cfg(tp);
  2616. rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
  2617. mutex_unlock(&tp->control);
  2618. usb_autopm_put_interface(tp->intf);
  2619. }
  2620. #ifdef CONFIG_PM_SLEEP
  2621. static int rtl_notifier(struct notifier_block *nb, unsigned long action,
  2622. void *data)
  2623. {
  2624. struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
  2625. switch (action) {
  2626. case PM_HIBERNATION_PREPARE:
  2627. case PM_SUSPEND_PREPARE:
  2628. usb_autopm_get_interface(tp->intf);
  2629. break;
  2630. case PM_POST_HIBERNATION:
  2631. case PM_POST_SUSPEND:
  2632. usb_autopm_put_interface(tp->intf);
  2633. break;
  2634. case PM_POST_RESTORE:
  2635. case PM_RESTORE_PREPARE:
  2636. default:
  2637. break;
  2638. }
  2639. return NOTIFY_DONE;
  2640. }
  2641. #endif
  2642. static int rtl8152_open(struct net_device *netdev)
  2643. {
  2644. struct r8152 *tp = netdev_priv(netdev);
  2645. int res = 0;
  2646. res = alloc_all_mem(tp);
  2647. if (res)
  2648. goto out;
  2649. res = usb_autopm_get_interface(tp->intf);
  2650. if (res < 0)
  2651. goto out_free;
  2652. mutex_lock(&tp->control);
  2653. tp->rtl_ops.up(tp);
  2654. netif_carrier_off(netdev);
  2655. netif_start_queue(netdev);
  2656. set_bit(WORK_ENABLE, &tp->flags);
  2657. res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2658. if (res) {
  2659. if (res == -ENODEV)
  2660. netif_device_detach(tp->netdev);
  2661. netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
  2662. res);
  2663. goto out_unlock;
  2664. }
  2665. napi_enable(&tp->napi);
  2666. mutex_unlock(&tp->control);
  2667. usb_autopm_put_interface(tp->intf);
  2668. #ifdef CONFIG_PM_SLEEP
  2669. tp->pm_notifier.notifier_call = rtl_notifier;
  2670. register_pm_notifier(&tp->pm_notifier);
  2671. #endif
  2672. return 0;
  2673. out_unlock:
  2674. mutex_unlock(&tp->control);
  2675. usb_autopm_put_interface(tp->intf);
  2676. out_free:
  2677. free_all_mem(tp);
  2678. out:
  2679. return res;
  2680. }
  2681. static int rtl8152_close(struct net_device *netdev)
  2682. {
  2683. struct r8152 *tp = netdev_priv(netdev);
  2684. int res = 0;
  2685. #ifdef CONFIG_PM_SLEEP
  2686. unregister_pm_notifier(&tp->pm_notifier);
  2687. #endif
  2688. napi_disable(&tp->napi);
  2689. clear_bit(WORK_ENABLE, &tp->flags);
  2690. usb_kill_urb(tp->intr_urb);
  2691. cancel_delayed_work_sync(&tp->schedule);
  2692. netif_stop_queue(netdev);
  2693. res = usb_autopm_get_interface(tp->intf);
  2694. if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2695. rtl_drop_queued_tx(tp);
  2696. rtl_stop_rx(tp);
  2697. } else {
  2698. mutex_lock(&tp->control);
  2699. tp->rtl_ops.down(tp);
  2700. mutex_unlock(&tp->control);
  2701. usb_autopm_put_interface(tp->intf);
  2702. }
  2703. free_all_mem(tp);
  2704. return res;
  2705. }
  2706. static void rtl_tally_reset(struct r8152 *tp)
  2707. {
  2708. u32 ocp_data;
  2709. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
  2710. ocp_data |= TALLY_RESET;
  2711. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
  2712. }
  2713. static void r8152b_init(struct r8152 *tp)
  2714. {
  2715. u32 ocp_data;
  2716. u16 data;
  2717. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2718. return;
  2719. data = r8152_mdio_read(tp, MII_BMCR);
  2720. if (data & BMCR_PDOWN) {
  2721. data &= ~BMCR_PDOWN;
  2722. r8152_mdio_write(tp, MII_BMCR, data);
  2723. }
  2724. r8152_aldps_en(tp, false);
  2725. if (tp->version == RTL_VER_01) {
  2726. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2727. ocp_data &= ~LED_MODE_MASK;
  2728. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2729. }
  2730. r8152_power_cut_en(tp, false);
  2731. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  2732. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  2733. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  2734. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  2735. ocp_data &= ~MCU_CLK_RATIO_MASK;
  2736. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  2737. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  2738. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  2739. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  2740. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  2741. rtl_tally_reset(tp);
  2742. /* enable rx aggregation */
  2743. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2744. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  2745. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2746. }
  2747. static void r8153_init(struct r8152 *tp)
  2748. {
  2749. u32 ocp_data;
  2750. u16 data;
  2751. int i;
  2752. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2753. return;
  2754. r8153_u1u2en(tp, false);
  2755. for (i = 0; i < 500; i++) {
  2756. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  2757. AUTOLOAD_DONE)
  2758. break;
  2759. msleep(20);
  2760. }
  2761. for (i = 0; i < 500; i++) {
  2762. ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
  2763. if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
  2764. break;
  2765. msleep(20);
  2766. }
  2767. if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
  2768. tp->version == RTL_VER_05)
  2769. ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
  2770. data = r8152_mdio_read(tp, MII_BMCR);
  2771. if (data & BMCR_PDOWN) {
  2772. data &= ~BMCR_PDOWN;
  2773. r8152_mdio_write(tp, MII_BMCR, data);
  2774. }
  2775. for (i = 0; i < 500; i++) {
  2776. ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
  2777. if (ocp_data == PHY_STAT_LAN_ON)
  2778. break;
  2779. msleep(20);
  2780. }
  2781. usb_disable_lpm(tp->udev);
  2782. r8153_u2p3en(tp, false);
  2783. if (tp->version == RTL_VER_04) {
  2784. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
  2785. ocp_data &= ~pwd_dn_scale_mask;
  2786. ocp_data |= pwd_dn_scale(96);
  2787. ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
  2788. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
  2789. ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
  2790. ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
  2791. } else if (tp->version == RTL_VER_05) {
  2792. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
  2793. ocp_data &= ~ECM_ALDPS;
  2794. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
  2795. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  2796. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  2797. ocp_data &= ~DYNAMIC_BURST;
  2798. else
  2799. ocp_data |= DYNAMIC_BURST;
  2800. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  2801. } else if (tp->version == RTL_VER_06) {
  2802. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  2803. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  2804. ocp_data &= ~DYNAMIC_BURST;
  2805. else
  2806. ocp_data |= DYNAMIC_BURST;
  2807. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  2808. }
  2809. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
  2810. ocp_data |= EP4_FULL_FC;
  2811. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
  2812. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
  2813. ocp_data &= ~TIMER11_EN;
  2814. ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
  2815. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2816. ocp_data &= ~LED_MODE_MASK;
  2817. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2818. ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
  2819. if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
  2820. ocp_data |= LPM_TIMER_500MS;
  2821. else
  2822. ocp_data |= LPM_TIMER_500US;
  2823. ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
  2824. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
  2825. ocp_data &= ~SEN_VAL_MASK;
  2826. ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
  2827. ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
  2828. ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
  2829. r8153_power_cut_en(tp, false);
  2830. r8153_u1u2en(tp, true);
  2831. /* MAC clock speed down */
  2832. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
  2833. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
  2834. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
  2835. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
  2836. rtl_tally_reset(tp);
  2837. r8153_u2p3en(tp, true);
  2838. }
  2839. static int rtl8152_pre_reset(struct usb_interface *intf)
  2840. {
  2841. struct r8152 *tp = usb_get_intfdata(intf);
  2842. struct net_device *netdev;
  2843. if (!tp)
  2844. return 0;
  2845. netdev = tp->netdev;
  2846. if (!netif_running(netdev))
  2847. return 0;
  2848. netif_stop_queue(netdev);
  2849. napi_disable(&tp->napi);
  2850. clear_bit(WORK_ENABLE, &tp->flags);
  2851. usb_kill_urb(tp->intr_urb);
  2852. cancel_delayed_work_sync(&tp->schedule);
  2853. if (netif_carrier_ok(netdev)) {
  2854. mutex_lock(&tp->control);
  2855. tp->rtl_ops.disable(tp);
  2856. mutex_unlock(&tp->control);
  2857. }
  2858. return 0;
  2859. }
  2860. static int rtl8152_post_reset(struct usb_interface *intf)
  2861. {
  2862. struct r8152 *tp = usb_get_intfdata(intf);
  2863. struct net_device *netdev;
  2864. if (!tp)
  2865. return 0;
  2866. netdev = tp->netdev;
  2867. if (!netif_running(netdev))
  2868. return 0;
  2869. set_bit(WORK_ENABLE, &tp->flags);
  2870. if (netif_carrier_ok(netdev)) {
  2871. mutex_lock(&tp->control);
  2872. tp->rtl_ops.enable(tp);
  2873. rtl_start_rx(tp);
  2874. rtl8152_set_rx_mode(netdev);
  2875. mutex_unlock(&tp->control);
  2876. }
  2877. napi_enable(&tp->napi);
  2878. netif_wake_queue(netdev);
  2879. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2880. if (!list_empty(&tp->rx_done))
  2881. napi_schedule(&tp->napi);
  2882. return 0;
  2883. }
  2884. static bool delay_autosuspend(struct r8152 *tp)
  2885. {
  2886. bool sw_linking = !!netif_carrier_ok(tp->netdev);
  2887. bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
  2888. /* This means a linking change occurs and the driver doesn't detect it,
  2889. * yet. If the driver has disabled tx/rx and hw is linking on, the
  2890. * device wouldn't wake up by receiving any packet.
  2891. */
  2892. if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
  2893. return true;
  2894. /* If the linking down is occurred by nway, the device may miss the
  2895. * linking change event. And it wouldn't wake when linking on.
  2896. */
  2897. if (!sw_linking && tp->rtl_ops.in_nway(tp))
  2898. return true;
  2899. else if (!skb_queue_empty(&tp->tx_queue))
  2900. return true;
  2901. else
  2902. return false;
  2903. }
  2904. static int rtl8152_runtime_suspend(struct r8152 *tp)
  2905. {
  2906. struct net_device *netdev = tp->netdev;
  2907. int ret = 0;
  2908. set_bit(SELECTIVE_SUSPEND, &tp->flags);
  2909. smp_mb__after_atomic();
  2910. if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
  2911. u32 rcr = 0;
  2912. if (delay_autosuspend(tp)) {
  2913. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2914. smp_mb__after_atomic();
  2915. ret = -EBUSY;
  2916. goto out1;
  2917. }
  2918. if (netif_carrier_ok(netdev)) {
  2919. u32 ocp_data;
  2920. rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2921. ocp_data = rcr & ~RCR_ACPT_ALL;
  2922. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2923. rxdy_gated_en(tp, true);
  2924. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
  2925. PLA_OOB_CTRL);
  2926. if (!(ocp_data & RXFIFO_EMPTY)) {
  2927. rxdy_gated_en(tp, false);
  2928. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
  2929. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2930. smp_mb__after_atomic();
  2931. ret = -EBUSY;
  2932. goto out1;
  2933. }
  2934. }
  2935. clear_bit(WORK_ENABLE, &tp->flags);
  2936. usb_kill_urb(tp->intr_urb);
  2937. tp->rtl_ops.autosuspend_en(tp, true);
  2938. if (netif_carrier_ok(netdev)) {
  2939. struct napi_struct *napi = &tp->napi;
  2940. napi_disable(napi);
  2941. rtl_stop_rx(tp);
  2942. rxdy_gated_en(tp, false);
  2943. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
  2944. napi_enable(napi);
  2945. }
  2946. }
  2947. out1:
  2948. return ret;
  2949. }
  2950. static int rtl8152_system_suspend(struct r8152 *tp)
  2951. {
  2952. struct net_device *netdev = tp->netdev;
  2953. int ret = 0;
  2954. netif_device_detach(netdev);
  2955. if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
  2956. struct napi_struct *napi = &tp->napi;
  2957. clear_bit(WORK_ENABLE, &tp->flags);
  2958. usb_kill_urb(tp->intr_urb);
  2959. napi_disable(napi);
  2960. cancel_delayed_work_sync(&tp->schedule);
  2961. tp->rtl_ops.down(tp);
  2962. napi_enable(napi);
  2963. }
  2964. return ret;
  2965. }
  2966. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  2967. {
  2968. struct r8152 *tp = usb_get_intfdata(intf);
  2969. int ret;
  2970. mutex_lock(&tp->control);
  2971. if (PMSG_IS_AUTO(message))
  2972. ret = rtl8152_runtime_suspend(tp);
  2973. else
  2974. ret = rtl8152_system_suspend(tp);
  2975. mutex_unlock(&tp->control);
  2976. return ret;
  2977. }
  2978. static int rtl8152_resume(struct usb_interface *intf)
  2979. {
  2980. struct r8152 *tp = usb_get_intfdata(intf);
  2981. struct net_device *netdev = tp->netdev;
  2982. mutex_lock(&tp->control);
  2983. if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2984. tp->rtl_ops.init(tp);
  2985. queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
  2986. netif_device_attach(netdev);
  2987. }
  2988. if (netif_running(netdev) && netdev->flags & IFF_UP) {
  2989. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2990. struct napi_struct *napi = &tp->napi;
  2991. tp->rtl_ops.autosuspend_en(tp, false);
  2992. napi_disable(napi);
  2993. set_bit(WORK_ENABLE, &tp->flags);
  2994. if (netif_carrier_ok(netdev)) {
  2995. if (rtl8152_get_speed(tp) & LINK_STATUS) {
  2996. rtl_start_rx(tp);
  2997. } else {
  2998. netif_carrier_off(netdev);
  2999. tp->rtl_ops.disable(tp);
  3000. netif_info(tp, link, netdev,
  3001. "linking down\n");
  3002. }
  3003. }
  3004. napi_enable(napi);
  3005. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  3006. smp_mb__after_atomic();
  3007. if (!list_empty(&tp->rx_done))
  3008. napi_schedule(&tp->napi);
  3009. } else {
  3010. tp->rtl_ops.up(tp);
  3011. netif_carrier_off(netdev);
  3012. set_bit(WORK_ENABLE, &tp->flags);
  3013. }
  3014. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  3015. } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  3016. if (netdev->flags & IFF_UP)
  3017. tp->rtl_ops.autosuspend_en(tp, false);
  3018. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  3019. }
  3020. mutex_unlock(&tp->control);
  3021. return 0;
  3022. }
  3023. static int rtl8152_reset_resume(struct usb_interface *intf)
  3024. {
  3025. struct r8152 *tp = usb_get_intfdata(intf);
  3026. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  3027. return rtl8152_resume(intf);
  3028. }
  3029. static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  3030. {
  3031. struct r8152 *tp = netdev_priv(dev);
  3032. if (usb_autopm_get_interface(tp->intf) < 0)
  3033. return;
  3034. if (!rtl_can_wakeup(tp)) {
  3035. wol->supported = 0;
  3036. wol->wolopts = 0;
  3037. } else {
  3038. mutex_lock(&tp->control);
  3039. wol->supported = WAKE_ANY;
  3040. wol->wolopts = __rtl_get_wol(tp);
  3041. mutex_unlock(&tp->control);
  3042. }
  3043. usb_autopm_put_interface(tp->intf);
  3044. }
  3045. static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  3046. {
  3047. struct r8152 *tp = netdev_priv(dev);
  3048. int ret;
  3049. if (!rtl_can_wakeup(tp))
  3050. return -EOPNOTSUPP;
  3051. ret = usb_autopm_get_interface(tp->intf);
  3052. if (ret < 0)
  3053. goto out_set_wol;
  3054. mutex_lock(&tp->control);
  3055. __rtl_set_wol(tp, wol->wolopts);
  3056. tp->saved_wolopts = wol->wolopts & WAKE_ANY;
  3057. mutex_unlock(&tp->control);
  3058. usb_autopm_put_interface(tp->intf);
  3059. out_set_wol:
  3060. return ret;
  3061. }
  3062. static u32 rtl8152_get_msglevel(struct net_device *dev)
  3063. {
  3064. struct r8152 *tp = netdev_priv(dev);
  3065. return tp->msg_enable;
  3066. }
  3067. static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
  3068. {
  3069. struct r8152 *tp = netdev_priv(dev);
  3070. tp->msg_enable = value;
  3071. }
  3072. static void rtl8152_get_drvinfo(struct net_device *netdev,
  3073. struct ethtool_drvinfo *info)
  3074. {
  3075. struct r8152 *tp = netdev_priv(netdev);
  3076. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  3077. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  3078. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  3079. }
  3080. static
  3081. int rtl8152_get_link_ksettings(struct net_device *netdev,
  3082. struct ethtool_link_ksettings *cmd)
  3083. {
  3084. struct r8152 *tp = netdev_priv(netdev);
  3085. int ret;
  3086. if (!tp->mii.mdio_read)
  3087. return -EOPNOTSUPP;
  3088. ret = usb_autopm_get_interface(tp->intf);
  3089. if (ret < 0)
  3090. goto out;
  3091. mutex_lock(&tp->control);
  3092. ret = mii_ethtool_get_link_ksettings(&tp->mii, cmd);
  3093. mutex_unlock(&tp->control);
  3094. usb_autopm_put_interface(tp->intf);
  3095. out:
  3096. return ret;
  3097. }
  3098. static int rtl8152_set_link_ksettings(struct net_device *dev,
  3099. const struct ethtool_link_ksettings *cmd)
  3100. {
  3101. struct r8152 *tp = netdev_priv(dev);
  3102. int ret;
  3103. ret = usb_autopm_get_interface(tp->intf);
  3104. if (ret < 0)
  3105. goto out;
  3106. mutex_lock(&tp->control);
  3107. ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
  3108. cmd->base.duplex);
  3109. if (!ret) {
  3110. tp->autoneg = cmd->base.autoneg;
  3111. tp->speed = cmd->base.speed;
  3112. tp->duplex = cmd->base.duplex;
  3113. }
  3114. mutex_unlock(&tp->control);
  3115. usb_autopm_put_interface(tp->intf);
  3116. out:
  3117. return ret;
  3118. }
  3119. static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
  3120. "tx_packets",
  3121. "rx_packets",
  3122. "tx_errors",
  3123. "rx_errors",
  3124. "rx_missed",
  3125. "align_errors",
  3126. "tx_single_collisions",
  3127. "tx_multi_collisions",
  3128. "rx_unicast",
  3129. "rx_broadcast",
  3130. "rx_multicast",
  3131. "tx_aborted",
  3132. "tx_underrun",
  3133. };
  3134. static int rtl8152_get_sset_count(struct net_device *dev, int sset)
  3135. {
  3136. switch (sset) {
  3137. case ETH_SS_STATS:
  3138. return ARRAY_SIZE(rtl8152_gstrings);
  3139. default:
  3140. return -EOPNOTSUPP;
  3141. }
  3142. }
  3143. static void rtl8152_get_ethtool_stats(struct net_device *dev,
  3144. struct ethtool_stats *stats, u64 *data)
  3145. {
  3146. struct r8152 *tp = netdev_priv(dev);
  3147. struct tally_counter tally;
  3148. if (usb_autopm_get_interface(tp->intf) < 0)
  3149. return;
  3150. generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
  3151. usb_autopm_put_interface(tp->intf);
  3152. data[0] = le64_to_cpu(tally.tx_packets);
  3153. data[1] = le64_to_cpu(tally.rx_packets);
  3154. data[2] = le64_to_cpu(tally.tx_errors);
  3155. data[3] = le32_to_cpu(tally.rx_errors);
  3156. data[4] = le16_to_cpu(tally.rx_missed);
  3157. data[5] = le16_to_cpu(tally.align_errors);
  3158. data[6] = le32_to_cpu(tally.tx_one_collision);
  3159. data[7] = le32_to_cpu(tally.tx_multi_collision);
  3160. data[8] = le64_to_cpu(tally.rx_unicast);
  3161. data[9] = le64_to_cpu(tally.rx_broadcast);
  3162. data[10] = le32_to_cpu(tally.rx_multicast);
  3163. data[11] = le16_to_cpu(tally.tx_aborted);
  3164. data[12] = le16_to_cpu(tally.tx_underrun);
  3165. }
  3166. static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  3167. {
  3168. switch (stringset) {
  3169. case ETH_SS_STATS:
  3170. memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
  3171. break;
  3172. }
  3173. }
  3174. static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3175. {
  3176. u32 ocp_data, lp, adv, supported = 0;
  3177. u16 val;
  3178. val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  3179. supported = mmd_eee_cap_to_ethtool_sup_t(val);
  3180. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  3181. adv = mmd_eee_adv_to_ethtool_adv_t(val);
  3182. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  3183. lp = mmd_eee_adv_to_ethtool_adv_t(val);
  3184. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  3185. ocp_data &= EEE_RX_EN | EEE_TX_EN;
  3186. eee->eee_enabled = !!ocp_data;
  3187. eee->eee_active = !!(supported & adv & lp);
  3188. eee->supported = supported;
  3189. eee->advertised = adv;
  3190. eee->lp_advertised = lp;
  3191. return 0;
  3192. }
  3193. static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3194. {
  3195. u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
  3196. r8152_eee_en(tp, eee->eee_enabled);
  3197. if (!eee->eee_enabled)
  3198. val = 0;
  3199. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3200. return 0;
  3201. }
  3202. static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3203. {
  3204. u32 ocp_data, lp, adv, supported = 0;
  3205. u16 val;
  3206. val = ocp_reg_read(tp, OCP_EEE_ABLE);
  3207. supported = mmd_eee_cap_to_ethtool_sup_t(val);
  3208. val = ocp_reg_read(tp, OCP_EEE_ADV);
  3209. adv = mmd_eee_adv_to_ethtool_adv_t(val);
  3210. val = ocp_reg_read(tp, OCP_EEE_LPABLE);
  3211. lp = mmd_eee_adv_to_ethtool_adv_t(val);
  3212. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  3213. ocp_data &= EEE_RX_EN | EEE_TX_EN;
  3214. eee->eee_enabled = !!ocp_data;
  3215. eee->eee_active = !!(supported & adv & lp);
  3216. eee->supported = supported;
  3217. eee->advertised = adv;
  3218. eee->lp_advertised = lp;
  3219. return 0;
  3220. }
  3221. static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3222. {
  3223. u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
  3224. r8153_eee_en(tp, eee->eee_enabled);
  3225. if (!eee->eee_enabled)
  3226. val = 0;
  3227. ocp_reg_write(tp, OCP_EEE_ADV, val);
  3228. return 0;
  3229. }
  3230. static int
  3231. rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
  3232. {
  3233. struct r8152 *tp = netdev_priv(net);
  3234. int ret;
  3235. ret = usb_autopm_get_interface(tp->intf);
  3236. if (ret < 0)
  3237. goto out;
  3238. mutex_lock(&tp->control);
  3239. ret = tp->rtl_ops.eee_get(tp, edata);
  3240. mutex_unlock(&tp->control);
  3241. usb_autopm_put_interface(tp->intf);
  3242. out:
  3243. return ret;
  3244. }
  3245. static int
  3246. rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
  3247. {
  3248. struct r8152 *tp = netdev_priv(net);
  3249. int ret;
  3250. ret = usb_autopm_get_interface(tp->intf);
  3251. if (ret < 0)
  3252. goto out;
  3253. mutex_lock(&tp->control);
  3254. ret = tp->rtl_ops.eee_set(tp, edata);
  3255. if (!ret)
  3256. ret = mii_nway_restart(&tp->mii);
  3257. mutex_unlock(&tp->control);
  3258. usb_autopm_put_interface(tp->intf);
  3259. out:
  3260. return ret;
  3261. }
  3262. static int rtl8152_nway_reset(struct net_device *dev)
  3263. {
  3264. struct r8152 *tp = netdev_priv(dev);
  3265. int ret;
  3266. ret = usb_autopm_get_interface(tp->intf);
  3267. if (ret < 0)
  3268. goto out;
  3269. mutex_lock(&tp->control);
  3270. ret = mii_nway_restart(&tp->mii);
  3271. mutex_unlock(&tp->control);
  3272. usb_autopm_put_interface(tp->intf);
  3273. out:
  3274. return ret;
  3275. }
  3276. static int rtl8152_get_coalesce(struct net_device *netdev,
  3277. struct ethtool_coalesce *coalesce)
  3278. {
  3279. struct r8152 *tp = netdev_priv(netdev);
  3280. switch (tp->version) {
  3281. case RTL_VER_01:
  3282. case RTL_VER_02:
  3283. return -EOPNOTSUPP;
  3284. default:
  3285. break;
  3286. }
  3287. coalesce->rx_coalesce_usecs = tp->coalesce;
  3288. return 0;
  3289. }
  3290. static int rtl8152_set_coalesce(struct net_device *netdev,
  3291. struct ethtool_coalesce *coalesce)
  3292. {
  3293. struct r8152 *tp = netdev_priv(netdev);
  3294. int ret;
  3295. switch (tp->version) {
  3296. case RTL_VER_01:
  3297. case RTL_VER_02:
  3298. return -EOPNOTSUPP;
  3299. default:
  3300. break;
  3301. }
  3302. if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
  3303. return -EINVAL;
  3304. ret = usb_autopm_get_interface(tp->intf);
  3305. if (ret < 0)
  3306. return ret;
  3307. mutex_lock(&tp->control);
  3308. if (tp->coalesce != coalesce->rx_coalesce_usecs) {
  3309. tp->coalesce = coalesce->rx_coalesce_usecs;
  3310. if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
  3311. r8153_set_rx_early_timeout(tp);
  3312. }
  3313. mutex_unlock(&tp->control);
  3314. usb_autopm_put_interface(tp->intf);
  3315. return ret;
  3316. }
  3317. static const struct ethtool_ops ops = {
  3318. .get_drvinfo = rtl8152_get_drvinfo,
  3319. .get_link = ethtool_op_get_link,
  3320. .nway_reset = rtl8152_nway_reset,
  3321. .get_msglevel = rtl8152_get_msglevel,
  3322. .set_msglevel = rtl8152_set_msglevel,
  3323. .get_wol = rtl8152_get_wol,
  3324. .set_wol = rtl8152_set_wol,
  3325. .get_strings = rtl8152_get_strings,
  3326. .get_sset_count = rtl8152_get_sset_count,
  3327. .get_ethtool_stats = rtl8152_get_ethtool_stats,
  3328. .get_coalesce = rtl8152_get_coalesce,
  3329. .set_coalesce = rtl8152_set_coalesce,
  3330. .get_eee = rtl_ethtool_get_eee,
  3331. .set_eee = rtl_ethtool_set_eee,
  3332. .get_link_ksettings = rtl8152_get_link_ksettings,
  3333. .set_link_ksettings = rtl8152_set_link_ksettings,
  3334. };
  3335. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  3336. {
  3337. struct r8152 *tp = netdev_priv(netdev);
  3338. struct mii_ioctl_data *data = if_mii(rq);
  3339. int res;
  3340. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3341. return -ENODEV;
  3342. res = usb_autopm_get_interface(tp->intf);
  3343. if (res < 0)
  3344. goto out;
  3345. switch (cmd) {
  3346. case SIOCGMIIPHY:
  3347. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  3348. break;
  3349. case SIOCGMIIREG:
  3350. mutex_lock(&tp->control);
  3351. data->val_out = r8152_mdio_read(tp, data->reg_num);
  3352. mutex_unlock(&tp->control);
  3353. break;
  3354. case SIOCSMIIREG:
  3355. if (!capable(CAP_NET_ADMIN)) {
  3356. res = -EPERM;
  3357. break;
  3358. }
  3359. mutex_lock(&tp->control);
  3360. r8152_mdio_write(tp, data->reg_num, data->val_in);
  3361. mutex_unlock(&tp->control);
  3362. break;
  3363. default:
  3364. res = -EOPNOTSUPP;
  3365. }
  3366. usb_autopm_put_interface(tp->intf);
  3367. out:
  3368. return res;
  3369. }
  3370. static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
  3371. {
  3372. struct r8152 *tp = netdev_priv(dev);
  3373. int ret;
  3374. switch (tp->version) {
  3375. case RTL_VER_01:
  3376. case RTL_VER_02:
  3377. dev->mtu = new_mtu;
  3378. return 0;
  3379. default:
  3380. break;
  3381. }
  3382. ret = usb_autopm_get_interface(tp->intf);
  3383. if (ret < 0)
  3384. return ret;
  3385. mutex_lock(&tp->control);
  3386. dev->mtu = new_mtu;
  3387. if (netif_running(dev)) {
  3388. u32 rms = new_mtu + VLAN_ETH_HLEN + CRC_SIZE;
  3389. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
  3390. if (netif_carrier_ok(dev))
  3391. r8153_set_rx_early_size(tp);
  3392. }
  3393. mutex_unlock(&tp->control);
  3394. usb_autopm_put_interface(tp->intf);
  3395. return ret;
  3396. }
  3397. static const struct net_device_ops rtl8152_netdev_ops = {
  3398. .ndo_open = rtl8152_open,
  3399. .ndo_stop = rtl8152_close,
  3400. .ndo_do_ioctl = rtl8152_ioctl,
  3401. .ndo_start_xmit = rtl8152_start_xmit,
  3402. .ndo_tx_timeout = rtl8152_tx_timeout,
  3403. .ndo_set_features = rtl8152_set_features,
  3404. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  3405. .ndo_set_mac_address = rtl8152_set_mac_address,
  3406. .ndo_change_mtu = rtl8152_change_mtu,
  3407. .ndo_validate_addr = eth_validate_addr,
  3408. .ndo_features_check = rtl8152_features_check,
  3409. };
  3410. static void rtl8152_unload(struct r8152 *tp)
  3411. {
  3412. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3413. return;
  3414. if (tp->version != RTL_VER_01)
  3415. r8152_power_cut_en(tp, true);
  3416. }
  3417. static void rtl8153_unload(struct r8152 *tp)
  3418. {
  3419. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3420. return;
  3421. r8153_power_cut_en(tp, false);
  3422. }
  3423. static int rtl_ops_init(struct r8152 *tp)
  3424. {
  3425. struct rtl_ops *ops = &tp->rtl_ops;
  3426. int ret = 0;
  3427. switch (tp->version) {
  3428. case RTL_VER_01:
  3429. case RTL_VER_02:
  3430. ops->init = r8152b_init;
  3431. ops->enable = rtl8152_enable;
  3432. ops->disable = rtl8152_disable;
  3433. ops->up = rtl8152_up;
  3434. ops->down = rtl8152_down;
  3435. ops->unload = rtl8152_unload;
  3436. ops->eee_get = r8152_get_eee;
  3437. ops->eee_set = r8152_set_eee;
  3438. ops->in_nway = rtl8152_in_nway;
  3439. ops->hw_phy_cfg = r8152b_hw_phy_cfg;
  3440. ops->autosuspend_en = rtl_runtime_suspend_enable;
  3441. break;
  3442. case RTL_VER_03:
  3443. case RTL_VER_04:
  3444. case RTL_VER_05:
  3445. case RTL_VER_06:
  3446. ops->init = r8153_init;
  3447. ops->enable = rtl8153_enable;
  3448. ops->disable = rtl8153_disable;
  3449. ops->up = rtl8153_up;
  3450. ops->down = rtl8153_down;
  3451. ops->unload = rtl8153_unload;
  3452. ops->eee_get = r8153_get_eee;
  3453. ops->eee_set = r8153_set_eee;
  3454. ops->in_nway = rtl8153_in_nway;
  3455. ops->hw_phy_cfg = r8153_hw_phy_cfg;
  3456. ops->autosuspend_en = rtl8153_runtime_enable;
  3457. break;
  3458. default:
  3459. ret = -ENODEV;
  3460. netif_err(tp, probe, tp->netdev, "Unknown Device\n");
  3461. break;
  3462. }
  3463. return ret;
  3464. }
  3465. static u8 rtl_get_version(struct usb_interface *intf)
  3466. {
  3467. struct usb_device *udev = interface_to_usbdev(intf);
  3468. u32 ocp_data = 0;
  3469. __le32 *tmp;
  3470. u8 version;
  3471. int ret;
  3472. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  3473. if (!tmp)
  3474. return 0;
  3475. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  3476. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  3477. PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
  3478. if (ret > 0)
  3479. ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
  3480. kfree(tmp);
  3481. switch (ocp_data) {
  3482. case 0x4c00:
  3483. version = RTL_VER_01;
  3484. break;
  3485. case 0x4c10:
  3486. version = RTL_VER_02;
  3487. break;
  3488. case 0x5c00:
  3489. version = RTL_VER_03;
  3490. break;
  3491. case 0x5c10:
  3492. version = RTL_VER_04;
  3493. break;
  3494. case 0x5c20:
  3495. version = RTL_VER_05;
  3496. break;
  3497. case 0x5c30:
  3498. version = RTL_VER_06;
  3499. break;
  3500. default:
  3501. version = RTL_VER_UNKNOWN;
  3502. dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
  3503. break;
  3504. }
  3505. return version;
  3506. }
  3507. static int rtl8152_probe(struct usb_interface *intf,
  3508. const struct usb_device_id *id)
  3509. {
  3510. struct usb_device *udev = interface_to_usbdev(intf);
  3511. u8 version = rtl_get_version(intf);
  3512. struct r8152 *tp;
  3513. struct net_device *netdev;
  3514. int ret;
  3515. if (version == RTL_VER_UNKNOWN)
  3516. return -ENODEV;
  3517. if (udev->actconfig->desc.bConfigurationValue != 1) {
  3518. usb_driver_set_configuration(udev, 1);
  3519. return -ENODEV;
  3520. }
  3521. usb_reset_device(udev);
  3522. netdev = alloc_etherdev(sizeof(struct r8152));
  3523. if (!netdev) {
  3524. dev_err(&intf->dev, "Out of memory\n");
  3525. return -ENOMEM;
  3526. }
  3527. SET_NETDEV_DEV(netdev, &intf->dev);
  3528. tp = netdev_priv(netdev);
  3529. tp->msg_enable = 0x7FFF;
  3530. tp->udev = udev;
  3531. tp->netdev = netdev;
  3532. tp->intf = intf;
  3533. tp->version = version;
  3534. switch (version) {
  3535. case RTL_VER_01:
  3536. case RTL_VER_02:
  3537. tp->mii.supports_gmii = 0;
  3538. break;
  3539. default:
  3540. tp->mii.supports_gmii = 1;
  3541. break;
  3542. }
  3543. ret = rtl_ops_init(tp);
  3544. if (ret)
  3545. goto out;
  3546. mutex_init(&tp->control);
  3547. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  3548. INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
  3549. netdev->netdev_ops = &rtl8152_netdev_ops;
  3550. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  3551. netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  3552. NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
  3553. NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
  3554. NETIF_F_HW_VLAN_CTAG_TX;
  3555. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  3556. NETIF_F_TSO | NETIF_F_FRAGLIST |
  3557. NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
  3558. NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
  3559. netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3560. NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
  3561. NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  3562. if (tp->version == RTL_VER_01) {
  3563. netdev->features &= ~NETIF_F_RXCSUM;
  3564. netdev->hw_features &= ~NETIF_F_RXCSUM;
  3565. }
  3566. netdev->ethtool_ops = &ops;
  3567. netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
  3568. /* MTU range: 68 - 1500 or 9194 */
  3569. netdev->min_mtu = ETH_MIN_MTU;
  3570. switch (tp->version) {
  3571. case RTL_VER_01:
  3572. case RTL_VER_02:
  3573. netdev->max_mtu = ETH_DATA_LEN;
  3574. break;
  3575. default:
  3576. netdev->max_mtu = RTL8153_MAX_MTU;
  3577. break;
  3578. }
  3579. tp->mii.dev = netdev;
  3580. tp->mii.mdio_read = read_mii_word;
  3581. tp->mii.mdio_write = write_mii_word;
  3582. tp->mii.phy_id_mask = 0x3f;
  3583. tp->mii.reg_num_mask = 0x1f;
  3584. tp->mii.phy_id = R8152_PHY_ID;
  3585. switch (udev->speed) {
  3586. case USB_SPEED_SUPER:
  3587. case USB_SPEED_SUPER_PLUS:
  3588. tp->coalesce = COALESCE_SUPER;
  3589. break;
  3590. case USB_SPEED_HIGH:
  3591. tp->coalesce = COALESCE_HIGH;
  3592. break;
  3593. default:
  3594. tp->coalesce = COALESCE_SLOW;
  3595. break;
  3596. }
  3597. tp->autoneg = AUTONEG_ENABLE;
  3598. tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
  3599. tp->duplex = DUPLEX_FULL;
  3600. intf->needs_remote_wakeup = 1;
  3601. tp->rtl_ops.init(tp);
  3602. queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
  3603. set_ethernet_addr(tp);
  3604. usb_set_intfdata(intf, tp);
  3605. netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
  3606. ret = register_netdev(netdev);
  3607. if (ret != 0) {
  3608. netif_err(tp, probe, netdev, "couldn't register the device\n");
  3609. goto out1;
  3610. }
  3611. if (!rtl_can_wakeup(tp))
  3612. __rtl_set_wol(tp, 0);
  3613. tp->saved_wolopts = __rtl_get_wol(tp);
  3614. if (tp->saved_wolopts)
  3615. device_set_wakeup_enable(&udev->dev, true);
  3616. else
  3617. device_set_wakeup_enable(&udev->dev, false);
  3618. netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
  3619. return 0;
  3620. out1:
  3621. netif_napi_del(&tp->napi);
  3622. usb_set_intfdata(intf, NULL);
  3623. out:
  3624. free_netdev(netdev);
  3625. return ret;
  3626. }
  3627. static void rtl8152_disconnect(struct usb_interface *intf)
  3628. {
  3629. struct r8152 *tp = usb_get_intfdata(intf);
  3630. usb_set_intfdata(intf, NULL);
  3631. if (tp) {
  3632. struct usb_device *udev = tp->udev;
  3633. if (udev->state == USB_STATE_NOTATTACHED)
  3634. set_bit(RTL8152_UNPLUG, &tp->flags);
  3635. netif_napi_del(&tp->napi);
  3636. unregister_netdev(tp->netdev);
  3637. cancel_delayed_work_sync(&tp->hw_phy_work);
  3638. tp->rtl_ops.unload(tp);
  3639. free_netdev(tp->netdev);
  3640. }
  3641. }
  3642. #define REALTEK_USB_DEVICE(vend, prod) \
  3643. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  3644. USB_DEVICE_ID_MATCH_INT_CLASS, \
  3645. .idVendor = (vend), \
  3646. .idProduct = (prod), \
  3647. .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
  3648. }, \
  3649. { \
  3650. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
  3651. USB_DEVICE_ID_MATCH_DEVICE, \
  3652. .idVendor = (vend), \
  3653. .idProduct = (prod), \
  3654. .bInterfaceClass = USB_CLASS_COMM, \
  3655. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
  3656. .bInterfaceProtocol = USB_CDC_PROTO_NONE
  3657. /* table of devices that work with this driver */
  3658. static struct usb_device_id rtl8152_table[] = {
  3659. {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
  3660. {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
  3661. {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
  3662. {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
  3663. {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
  3664. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
  3665. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
  3666. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
  3667. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
  3668. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
  3669. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
  3670. {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
  3671. {}
  3672. };
  3673. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  3674. static struct usb_driver rtl8152_driver = {
  3675. .name = MODULENAME,
  3676. .id_table = rtl8152_table,
  3677. .probe = rtl8152_probe,
  3678. .disconnect = rtl8152_disconnect,
  3679. .suspend = rtl8152_suspend,
  3680. .resume = rtl8152_resume,
  3681. .reset_resume = rtl8152_reset_resume,
  3682. .pre_reset = rtl8152_pre_reset,
  3683. .post_reset = rtl8152_post_reset,
  3684. .supports_autosuspend = 1,
  3685. .disable_hub_initiated_lpm = 1,
  3686. };
  3687. module_usb_driver(rtl8152_driver);
  3688. MODULE_AUTHOR(DRIVER_AUTHOR);
  3689. MODULE_DESCRIPTION(DRIVER_DESC);
  3690. MODULE_LICENSE("GPL");
  3691. MODULE_VERSION(DRIVER_VERSION);