rtl8xxxu_core.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405
  1. /*
  2. * RTL8XXXU mac80211 USB driver
  3. *
  4. * Copyright (c) 2014 - 2017 Jes Sorensen <Jes.Sorensen@gmail.com>
  5. *
  6. * Portions, notably calibration code:
  7. * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
  8. *
  9. * This driver was written as a replacement for the vendor provided
  10. * rtl8723au driver. As the Realtek 8xxx chips are very similar in
  11. * their programming interface, I have started adding support for
  12. * additional 8xxx chips like the 8192cu, 8188cus, etc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of version 2 of the GNU General Public License as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but WITHOUT
  19. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  21. * more details.
  22. */
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/errno.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/list.h>
  31. #include <linux/usb.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/wireless.h>
  36. #include <linux/firmware.h>
  37. #include <linux/moduleparam.h>
  38. #include <net/mac80211.h>
  39. #include "rtl8xxxu.h"
  40. #include "rtl8xxxu_regs.h"
  41. #define DRIVER_NAME "rtl8xxxu"
  42. int rtl8xxxu_debug = RTL8XXXU_DEBUG_EFUSE;
  43. static bool rtl8xxxu_ht40_2g;
  44. static bool rtl8xxxu_dma_aggregation;
  45. static int rtl8xxxu_dma_agg_timeout = -1;
  46. static int rtl8xxxu_dma_agg_pages = -1;
  47. MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
  48. MODULE_DESCRIPTION("RTL8XXXu USB mac80211 Wireless LAN Driver");
  49. MODULE_LICENSE("GPL");
  50. MODULE_FIRMWARE("rtlwifi/rtl8723aufw_A.bin");
  51. MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B.bin");
  52. MODULE_FIRMWARE("rtlwifi/rtl8723aufw_B_NoBT.bin");
  53. MODULE_FIRMWARE("rtlwifi/rtl8192cufw_A.bin");
  54. MODULE_FIRMWARE("rtlwifi/rtl8192cufw_B.bin");
  55. MODULE_FIRMWARE("rtlwifi/rtl8192cufw_TMSC.bin");
  56. MODULE_FIRMWARE("rtlwifi/rtl8192eu_nic.bin");
  57. MODULE_FIRMWARE("rtlwifi/rtl8723bu_nic.bin");
  58. MODULE_FIRMWARE("rtlwifi/rtl8723bu_bt.bin");
  59. module_param_named(debug, rtl8xxxu_debug, int, 0600);
  60. MODULE_PARM_DESC(debug, "Set debug mask");
  61. module_param_named(ht40_2g, rtl8xxxu_ht40_2g, bool, 0600);
  62. MODULE_PARM_DESC(ht40_2g, "Enable HT40 support on the 2.4GHz band");
  63. module_param_named(dma_aggregation, rtl8xxxu_dma_aggregation, bool, 0600);
  64. MODULE_PARM_DESC(dma_aggregation, "Enable DMA packet aggregation");
  65. module_param_named(dma_agg_timeout, rtl8xxxu_dma_agg_timeout, int, 0600);
  66. MODULE_PARM_DESC(dma_agg_timeout, "Set DMA aggregation timeout (range 1-127)");
  67. module_param_named(dma_agg_pages, rtl8xxxu_dma_agg_pages, int, 0600);
  68. MODULE_PARM_DESC(dma_agg_pages, "Set DMA aggregation pages (range 1-127, 0 to disable)");
  69. #define USB_VENDOR_ID_REALTEK 0x0bda
  70. #define RTL8XXXU_RX_URBS 32
  71. #define RTL8XXXU_RX_URB_PENDING_WATER 8
  72. #define RTL8XXXU_TX_URBS 64
  73. #define RTL8XXXU_TX_URB_LOW_WATER 25
  74. #define RTL8XXXU_TX_URB_HIGH_WATER 32
  75. static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
  76. struct rtl8xxxu_rx_urb *rx_urb);
  77. static struct ieee80211_rate rtl8xxxu_rates[] = {
  78. { .bitrate = 10, .hw_value = DESC_RATE_1M, .flags = 0 },
  79. { .bitrate = 20, .hw_value = DESC_RATE_2M, .flags = 0 },
  80. { .bitrate = 55, .hw_value = DESC_RATE_5_5M, .flags = 0 },
  81. { .bitrate = 110, .hw_value = DESC_RATE_11M, .flags = 0 },
  82. { .bitrate = 60, .hw_value = DESC_RATE_6M, .flags = 0 },
  83. { .bitrate = 90, .hw_value = DESC_RATE_9M, .flags = 0 },
  84. { .bitrate = 120, .hw_value = DESC_RATE_12M, .flags = 0 },
  85. { .bitrate = 180, .hw_value = DESC_RATE_18M, .flags = 0 },
  86. { .bitrate = 240, .hw_value = DESC_RATE_24M, .flags = 0 },
  87. { .bitrate = 360, .hw_value = DESC_RATE_36M, .flags = 0 },
  88. { .bitrate = 480, .hw_value = DESC_RATE_48M, .flags = 0 },
  89. { .bitrate = 540, .hw_value = DESC_RATE_54M, .flags = 0 },
  90. };
  91. static struct ieee80211_channel rtl8xxxu_channels_2g[] = {
  92. { .band = NL80211_BAND_2GHZ, .center_freq = 2412,
  93. .hw_value = 1, .max_power = 30 },
  94. { .band = NL80211_BAND_2GHZ, .center_freq = 2417,
  95. .hw_value = 2, .max_power = 30 },
  96. { .band = NL80211_BAND_2GHZ, .center_freq = 2422,
  97. .hw_value = 3, .max_power = 30 },
  98. { .band = NL80211_BAND_2GHZ, .center_freq = 2427,
  99. .hw_value = 4, .max_power = 30 },
  100. { .band = NL80211_BAND_2GHZ, .center_freq = 2432,
  101. .hw_value = 5, .max_power = 30 },
  102. { .band = NL80211_BAND_2GHZ, .center_freq = 2437,
  103. .hw_value = 6, .max_power = 30 },
  104. { .band = NL80211_BAND_2GHZ, .center_freq = 2442,
  105. .hw_value = 7, .max_power = 30 },
  106. { .band = NL80211_BAND_2GHZ, .center_freq = 2447,
  107. .hw_value = 8, .max_power = 30 },
  108. { .band = NL80211_BAND_2GHZ, .center_freq = 2452,
  109. .hw_value = 9, .max_power = 30 },
  110. { .band = NL80211_BAND_2GHZ, .center_freq = 2457,
  111. .hw_value = 10, .max_power = 30 },
  112. { .band = NL80211_BAND_2GHZ, .center_freq = 2462,
  113. .hw_value = 11, .max_power = 30 },
  114. { .band = NL80211_BAND_2GHZ, .center_freq = 2467,
  115. .hw_value = 12, .max_power = 30 },
  116. { .band = NL80211_BAND_2GHZ, .center_freq = 2472,
  117. .hw_value = 13, .max_power = 30 },
  118. { .band = NL80211_BAND_2GHZ, .center_freq = 2484,
  119. .hw_value = 14, .max_power = 30 }
  120. };
  121. static struct ieee80211_supported_band rtl8xxxu_supported_band = {
  122. .channels = rtl8xxxu_channels_2g,
  123. .n_channels = ARRAY_SIZE(rtl8xxxu_channels_2g),
  124. .bitrates = rtl8xxxu_rates,
  125. .n_bitrates = ARRAY_SIZE(rtl8xxxu_rates),
  126. };
  127. struct rtl8xxxu_reg8val rtl8xxxu_gen1_mac_init_table[] = {
  128. {0x420, 0x80}, {0x423, 0x00}, {0x430, 0x00}, {0x431, 0x00},
  129. {0x432, 0x00}, {0x433, 0x01}, {0x434, 0x04}, {0x435, 0x05},
  130. {0x436, 0x06}, {0x437, 0x07}, {0x438, 0x00}, {0x439, 0x00},
  131. {0x43a, 0x00}, {0x43b, 0x01}, {0x43c, 0x04}, {0x43d, 0x05},
  132. {0x43e, 0x06}, {0x43f, 0x07}, {0x440, 0x5d}, {0x441, 0x01},
  133. {0x442, 0x00}, {0x444, 0x15}, {0x445, 0xf0}, {0x446, 0x0f},
  134. {0x447, 0x00}, {0x458, 0x41}, {0x459, 0xa8}, {0x45a, 0x72},
  135. {0x45b, 0xb9}, {0x460, 0x66}, {0x461, 0x66}, {0x462, 0x08},
  136. {0x463, 0x03}, {0x4c8, 0xff}, {0x4c9, 0x08}, {0x4cc, 0xff},
  137. {0x4cd, 0xff}, {0x4ce, 0x01}, {0x500, 0x26}, {0x501, 0xa2},
  138. {0x502, 0x2f}, {0x503, 0x00}, {0x504, 0x28}, {0x505, 0xa3},
  139. {0x506, 0x5e}, {0x507, 0x00}, {0x508, 0x2b}, {0x509, 0xa4},
  140. {0x50a, 0x5e}, {0x50b, 0x00}, {0x50c, 0x4f}, {0x50d, 0xa4},
  141. {0x50e, 0x00}, {0x50f, 0x00}, {0x512, 0x1c}, {0x514, 0x0a},
  142. {0x515, 0x10}, {0x516, 0x0a}, {0x517, 0x10}, {0x51a, 0x16},
  143. {0x524, 0x0f}, {0x525, 0x4f}, {0x546, 0x40}, {0x547, 0x00},
  144. {0x550, 0x10}, {0x551, 0x10}, {0x559, 0x02}, {0x55a, 0x02},
  145. {0x55d, 0xff}, {0x605, 0x30}, {0x608, 0x0e}, {0x609, 0x2a},
  146. {0x652, 0x20}, {0x63c, 0x0a}, {0x63d, 0x0a}, {0x63e, 0x0e},
  147. {0x63f, 0x0e}, {0x66e, 0x05}, {0x700, 0x21}, {0x701, 0x43},
  148. {0x702, 0x65}, {0x703, 0x87}, {0x708, 0x21}, {0x709, 0x43},
  149. {0x70a, 0x65}, {0x70b, 0x87}, {0xffff, 0xff},
  150. };
  151. static struct rtl8xxxu_reg32val rtl8723a_phy_1t_init_table[] = {
  152. {0x800, 0x80040000}, {0x804, 0x00000003},
  153. {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
  154. {0x810, 0x10001331}, {0x814, 0x020c3d10},
  155. {0x818, 0x02200385}, {0x81c, 0x00000000},
  156. {0x820, 0x01000100}, {0x824, 0x00390004},
  157. {0x828, 0x00000000}, {0x82c, 0x00000000},
  158. {0x830, 0x00000000}, {0x834, 0x00000000},
  159. {0x838, 0x00000000}, {0x83c, 0x00000000},
  160. {0x840, 0x00010000}, {0x844, 0x00000000},
  161. {0x848, 0x00000000}, {0x84c, 0x00000000},
  162. {0x850, 0x00000000}, {0x854, 0x00000000},
  163. {0x858, 0x569a569a}, {0x85c, 0x001b25a4},
  164. {0x860, 0x66f60110}, {0x864, 0x061f0130},
  165. {0x868, 0x00000000}, {0x86c, 0x32323200},
  166. {0x870, 0x07000760}, {0x874, 0x22004000},
  167. {0x878, 0x00000808}, {0x87c, 0x00000000},
  168. {0x880, 0xc0083070}, {0x884, 0x000004d5},
  169. {0x888, 0x00000000}, {0x88c, 0xccc000c0},
  170. {0x890, 0x00000800}, {0x894, 0xfffffffe},
  171. {0x898, 0x40302010}, {0x89c, 0x00706050},
  172. {0x900, 0x00000000}, {0x904, 0x00000023},
  173. {0x908, 0x00000000}, {0x90c, 0x81121111},
  174. {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
  175. {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
  176. {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
  177. {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
  178. {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
  179. {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
  180. {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
  181. {0xa78, 0x00000900},
  182. {0xc00, 0x48071d40}, {0xc04, 0x03a05611},
  183. {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
  184. {0xc10, 0x08800000}, {0xc14, 0x40000100},
  185. {0xc18, 0x08800000}, {0xc1c, 0x40000100},
  186. {0xc20, 0x00000000}, {0xc24, 0x00000000},
  187. {0xc28, 0x00000000}, {0xc2c, 0x00000000},
  188. {0xc30, 0x69e9ac44}, {0xc34, 0x469652af},
  189. {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
  190. {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
  191. {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
  192. {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
  193. {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
  194. {0xc60, 0x00000000}, {0xc64, 0x7112848b},
  195. {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
  196. {0xc70, 0x2c7f000d}, {0xc74, 0x018610db},
  197. {0xc78, 0x0000001f}, {0xc7c, 0x00b91612},
  198. {0xc80, 0x40000100}, {0xc84, 0x20f60000},
  199. {0xc88, 0x40000100}, {0xc8c, 0x20200000},
  200. {0xc90, 0x00121820}, {0xc94, 0x00000000},
  201. {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
  202. {0xca0, 0x00000000}, {0xca4, 0x00000080},
  203. {0xca8, 0x00000000}, {0xcac, 0x00000000},
  204. {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
  205. {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
  206. {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
  207. {0xcc8, 0x00000000}, {0xccc, 0x00000000},
  208. {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
  209. {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
  210. {0xce0, 0x00222222}, {0xce4, 0x00000000},
  211. {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
  212. {0xd00, 0x00080740}, {0xd04, 0x00020401},
  213. {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
  214. {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
  215. {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
  216. {0xd30, 0x00000000}, {0xd34, 0x80608000},
  217. {0xd38, 0x00000000}, {0xd3c, 0x00027293},
  218. {0xd40, 0x00000000}, {0xd44, 0x00000000},
  219. {0xd48, 0x00000000}, {0xd4c, 0x00000000},
  220. {0xd50, 0x6437140a}, {0xd54, 0x00000000},
  221. {0xd58, 0x00000000}, {0xd5c, 0x30032064},
  222. {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
  223. {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
  224. {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
  225. {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
  226. {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
  227. {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
  228. {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
  229. {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
  230. {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
  231. {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
  232. {0xe44, 0x01004800}, {0xe48, 0xfb000000},
  233. {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
  234. {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
  235. {0xe5c, 0x28160d05}, {0xe60, 0x00000008},
  236. {0xe68, 0x001b25a4}, {0xe6c, 0x631b25a0},
  237. {0xe70, 0x631b25a0}, {0xe74, 0x081b25a0},
  238. {0xe78, 0x081b25a0}, {0xe7c, 0x081b25a0},
  239. {0xe80, 0x081b25a0}, {0xe84, 0x631b25a0},
  240. {0xe88, 0x081b25a0}, {0xe8c, 0x631b25a0},
  241. {0xed0, 0x631b25a0}, {0xed4, 0x631b25a0},
  242. {0xed8, 0x631b25a0}, {0xedc, 0x001b25a0},
  243. {0xee0, 0x001b25a0}, {0xeec, 0x6b1b25a0},
  244. {0xf14, 0x00000003}, {0xf4c, 0x00000000},
  245. {0xf00, 0x00000300},
  246. {0xffff, 0xffffffff},
  247. };
  248. static struct rtl8xxxu_reg32val rtl8192cu_phy_2t_init_table[] = {
  249. {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
  250. {0x800, 0x80040002}, {0x804, 0x00000003},
  251. {0x808, 0x0000fc00}, {0x80c, 0x0000000a},
  252. {0x810, 0x10000330}, {0x814, 0x020c3d10},
  253. {0x818, 0x02200385}, {0x81c, 0x00000000},
  254. {0x820, 0x01000100}, {0x824, 0x00390004},
  255. {0x828, 0x01000100}, {0x82c, 0x00390004},
  256. {0x830, 0x27272727}, {0x834, 0x27272727},
  257. {0x838, 0x27272727}, {0x83c, 0x27272727},
  258. {0x840, 0x00010000}, {0x844, 0x00010000},
  259. {0x848, 0x27272727}, {0x84c, 0x27272727},
  260. {0x850, 0x00000000}, {0x854, 0x00000000},
  261. {0x858, 0x569a569a}, {0x85c, 0x0c1b25a4},
  262. {0x860, 0x66e60230}, {0x864, 0x061f0130},
  263. {0x868, 0x27272727}, {0x86c, 0x2b2b2b27},
  264. {0x870, 0x07000700}, {0x874, 0x22184000},
  265. {0x878, 0x08080808}, {0x87c, 0x00000000},
  266. {0x880, 0xc0083070}, {0x884, 0x000004d5},
  267. {0x888, 0x00000000}, {0x88c, 0xcc0000c0},
  268. {0x890, 0x00000800}, {0x894, 0xfffffffe},
  269. {0x898, 0x40302010}, {0x89c, 0x00706050},
  270. {0x900, 0x00000000}, {0x904, 0x00000023},
  271. {0x908, 0x00000000}, {0x90c, 0x81121313},
  272. {0xa00, 0x00d047c8}, {0xa04, 0x80ff000c},
  273. {0xa08, 0x8c838300}, {0xa0c, 0x2e68120f},
  274. {0xa10, 0x9500bb78}, {0xa14, 0x11144028},
  275. {0xa18, 0x00881117}, {0xa1c, 0x89140f00},
  276. {0xa20, 0x1a1b0000}, {0xa24, 0x090e1317},
  277. {0xa28, 0x00000204}, {0xa2c, 0x00d30000},
  278. {0xa70, 0x101fbf00}, {0xa74, 0x00000007},
  279. {0xc00, 0x48071d40}, {0xc04, 0x03a05633},
  280. {0xc08, 0x000000e4}, {0xc0c, 0x6c6c6c6c},
  281. {0xc10, 0x08800000}, {0xc14, 0x40000100},
  282. {0xc18, 0x08800000}, {0xc1c, 0x40000100},
  283. {0xc20, 0x00000000}, {0xc24, 0x00000000},
  284. {0xc28, 0x00000000}, {0xc2c, 0x00000000},
  285. {0xc30, 0x69e9ac44}, {0xc34, 0x469652cf},
  286. {0xc38, 0x49795994}, {0xc3c, 0x0a97971c},
  287. {0xc40, 0x1f7c403f}, {0xc44, 0x000100b7},
  288. {0xc48, 0xec020107}, {0xc4c, 0x007f037f},
  289. {0xc50, 0x69543420}, {0xc54, 0x43bc0094},
  290. {0xc58, 0x69543420}, {0xc5c, 0x433c0094},
  291. {0xc60, 0x00000000}, {0xc64, 0x5116848b},
  292. {0xc68, 0x47c00bff}, {0xc6c, 0x00000036},
  293. {0xc70, 0x2c7f000d}, {0xc74, 0x2186115b},
  294. {0xc78, 0x0000001f}, {0xc7c, 0x00b99612},
  295. {0xc80, 0x40000100}, {0xc84, 0x20f60000},
  296. {0xc88, 0x40000100}, {0xc8c, 0xa0e40000},
  297. {0xc90, 0x00121820}, {0xc94, 0x00000000},
  298. {0xc98, 0x00121820}, {0xc9c, 0x00007f7f},
  299. {0xca0, 0x00000000}, {0xca4, 0x00000080},
  300. {0xca8, 0x00000000}, {0xcac, 0x00000000},
  301. {0xcb0, 0x00000000}, {0xcb4, 0x00000000},
  302. {0xcb8, 0x00000000}, {0xcbc, 0x28000000},
  303. {0xcc0, 0x00000000}, {0xcc4, 0x00000000},
  304. {0xcc8, 0x00000000}, {0xccc, 0x00000000},
  305. {0xcd0, 0x00000000}, {0xcd4, 0x00000000},
  306. {0xcd8, 0x64b22427}, {0xcdc, 0x00766932},
  307. {0xce0, 0x00222222}, {0xce4, 0x00000000},
  308. {0xce8, 0x37644302}, {0xcec, 0x2f97d40c},
  309. {0xd00, 0x00080740}, {0xd04, 0x00020403},
  310. {0xd08, 0x0000907f}, {0xd0c, 0x20010201},
  311. {0xd10, 0xa0633333}, {0xd14, 0x3333bc43},
  312. {0xd18, 0x7a8f5b6b}, {0xd2c, 0xcc979975},
  313. {0xd30, 0x00000000}, {0xd34, 0x80608000},
  314. {0xd38, 0x00000000}, {0xd3c, 0x00027293},
  315. {0xd40, 0x00000000}, {0xd44, 0x00000000},
  316. {0xd48, 0x00000000}, {0xd4c, 0x00000000},
  317. {0xd50, 0x6437140a}, {0xd54, 0x00000000},
  318. {0xd58, 0x00000000}, {0xd5c, 0x30032064},
  319. {0xd60, 0x4653de68}, {0xd64, 0x04518a3c},
  320. {0xd68, 0x00002101}, {0xd6c, 0x2a201c16},
  321. {0xd70, 0x1812362e}, {0xd74, 0x322c2220},
  322. {0xd78, 0x000e3c24}, {0xe00, 0x2a2a2a2a},
  323. {0xe04, 0x2a2a2a2a}, {0xe08, 0x03902a2a},
  324. {0xe10, 0x2a2a2a2a}, {0xe14, 0x2a2a2a2a},
  325. {0xe18, 0x2a2a2a2a}, {0xe1c, 0x2a2a2a2a},
  326. {0xe28, 0x00000000}, {0xe30, 0x1000dc1f},
  327. {0xe34, 0x10008c1f}, {0xe38, 0x02140102},
  328. {0xe3c, 0x681604c2}, {0xe40, 0x01007c00},
  329. {0xe44, 0x01004800}, {0xe48, 0xfb000000},
  330. {0xe4c, 0x000028d1}, {0xe50, 0x1000dc1f},
  331. {0xe54, 0x10008c1f}, {0xe58, 0x02140102},
  332. {0xe5c, 0x28160d05}, {0xe60, 0x00000010},
  333. {0xe68, 0x001b25a4}, {0xe6c, 0x63db25a4},
  334. {0xe70, 0x63db25a4}, {0xe74, 0x0c1b25a4},
  335. {0xe78, 0x0c1b25a4}, {0xe7c, 0x0c1b25a4},
  336. {0xe80, 0x0c1b25a4}, {0xe84, 0x63db25a4},
  337. {0xe88, 0x0c1b25a4}, {0xe8c, 0x63db25a4},
  338. {0xed0, 0x63db25a4}, {0xed4, 0x63db25a4},
  339. {0xed8, 0x63db25a4}, {0xedc, 0x001b25a4},
  340. {0xee0, 0x001b25a4}, {0xeec, 0x6fdb25a4},
  341. {0xf14, 0x00000003}, {0xf4c, 0x00000000},
  342. {0xf00, 0x00000300},
  343. {0xffff, 0xffffffff},
  344. };
  345. static struct rtl8xxxu_reg32val rtl8188ru_phy_1t_highpa_table[] = {
  346. {0x024, 0x0011800f}, {0x028, 0x00ffdb83},
  347. {0x040, 0x000c0004}, {0x800, 0x80040000},
  348. {0x804, 0x00000001}, {0x808, 0x0000fc00},
  349. {0x80c, 0x0000000a}, {0x810, 0x10005388},
  350. {0x814, 0x020c3d10}, {0x818, 0x02200385},
  351. {0x81c, 0x00000000}, {0x820, 0x01000100},
  352. {0x824, 0x00390204}, {0x828, 0x00000000},
  353. {0x82c, 0x00000000}, {0x830, 0x00000000},
  354. {0x834, 0x00000000}, {0x838, 0x00000000},
  355. {0x83c, 0x00000000}, {0x840, 0x00010000},
  356. {0x844, 0x00000000}, {0x848, 0x00000000},
  357. {0x84c, 0x00000000}, {0x850, 0x00000000},
  358. {0x854, 0x00000000}, {0x858, 0x569a569a},
  359. {0x85c, 0x001b25a4}, {0x860, 0x66e60230},
  360. {0x864, 0x061f0130}, {0x868, 0x00000000},
  361. {0x86c, 0x20202000}, {0x870, 0x03000300},
  362. {0x874, 0x22004000}, {0x878, 0x00000808},
  363. {0x87c, 0x00ffc3f1}, {0x880, 0xc0083070},
  364. {0x884, 0x000004d5}, {0x888, 0x00000000},
  365. {0x88c, 0xccc000c0}, {0x890, 0x00000800},
  366. {0x894, 0xfffffffe}, {0x898, 0x40302010},
  367. {0x89c, 0x00706050}, {0x900, 0x00000000},
  368. {0x904, 0x00000023}, {0x908, 0x00000000},
  369. {0x90c, 0x81121111}, {0xa00, 0x00d047c8},
  370. {0xa04, 0x80ff000c}, {0xa08, 0x8c838300},
  371. {0xa0c, 0x2e68120f}, {0xa10, 0x9500bb78},
  372. {0xa14, 0x11144028}, {0xa18, 0x00881117},
  373. {0xa1c, 0x89140f00}, {0xa20, 0x15160000},
  374. {0xa24, 0x070b0f12}, {0xa28, 0x00000104},
  375. {0xa2c, 0x00d30000}, {0xa70, 0x101fbf00},
  376. {0xa74, 0x00000007}, {0xc00, 0x48071d40},
  377. {0xc04, 0x03a05611}, {0xc08, 0x000000e4},
  378. {0xc0c, 0x6c6c6c6c}, {0xc10, 0x08800000},
  379. {0xc14, 0x40000100}, {0xc18, 0x08800000},
  380. {0xc1c, 0x40000100}, {0xc20, 0x00000000},
  381. {0xc24, 0x00000000}, {0xc28, 0x00000000},
  382. {0xc2c, 0x00000000}, {0xc30, 0x69e9ac44},
  383. {0xc34, 0x469652cf}, {0xc38, 0x49795994},
  384. {0xc3c, 0x0a97971c}, {0xc40, 0x1f7c403f},
  385. {0xc44, 0x000100b7}, {0xc48, 0xec020107},
  386. {0xc4c, 0x007f037f}, {0xc50, 0x6954342e},
  387. {0xc54, 0x43bc0094}, {0xc58, 0x6954342f},
  388. {0xc5c, 0x433c0094}, {0xc60, 0x00000000},
  389. {0xc64, 0x5116848b}, {0xc68, 0x47c00bff},
  390. {0xc6c, 0x00000036}, {0xc70, 0x2c46000d},
  391. {0xc74, 0x018610db}, {0xc78, 0x0000001f},
  392. {0xc7c, 0x00b91612}, {0xc80, 0x24000090},
  393. {0xc84, 0x20f60000}, {0xc88, 0x24000090},
  394. {0xc8c, 0x20200000}, {0xc90, 0x00121820},
  395. {0xc94, 0x00000000}, {0xc98, 0x00121820},
  396. {0xc9c, 0x00007f7f}, {0xca0, 0x00000000},
  397. {0xca4, 0x00000080}, {0xca8, 0x00000000},
  398. {0xcac, 0x00000000}, {0xcb0, 0x00000000},
  399. {0xcb4, 0x00000000}, {0xcb8, 0x00000000},
  400. {0xcbc, 0x28000000}, {0xcc0, 0x00000000},
  401. {0xcc4, 0x00000000}, {0xcc8, 0x00000000},
  402. {0xccc, 0x00000000}, {0xcd0, 0x00000000},
  403. {0xcd4, 0x00000000}, {0xcd8, 0x64b22427},
  404. {0xcdc, 0x00766932}, {0xce0, 0x00222222},
  405. {0xce4, 0x00000000}, {0xce8, 0x37644302},
  406. {0xcec, 0x2f97d40c}, {0xd00, 0x00080740},
  407. {0xd04, 0x00020401}, {0xd08, 0x0000907f},
  408. {0xd0c, 0x20010201}, {0xd10, 0xa0633333},
  409. {0xd14, 0x3333bc43}, {0xd18, 0x7a8f5b6b},
  410. {0xd2c, 0xcc979975}, {0xd30, 0x00000000},
  411. {0xd34, 0x80608000}, {0xd38, 0x00000000},
  412. {0xd3c, 0x00027293}, {0xd40, 0x00000000},
  413. {0xd44, 0x00000000}, {0xd48, 0x00000000},
  414. {0xd4c, 0x00000000}, {0xd50, 0x6437140a},
  415. {0xd54, 0x00000000}, {0xd58, 0x00000000},
  416. {0xd5c, 0x30032064}, {0xd60, 0x4653de68},
  417. {0xd64, 0x04518a3c}, {0xd68, 0x00002101},
  418. {0xd6c, 0x2a201c16}, {0xd70, 0x1812362e},
  419. {0xd74, 0x322c2220}, {0xd78, 0x000e3c24},
  420. {0xe00, 0x24242424}, {0xe04, 0x24242424},
  421. {0xe08, 0x03902024}, {0xe10, 0x24242424},
  422. {0xe14, 0x24242424}, {0xe18, 0x24242424},
  423. {0xe1c, 0x24242424}, {0xe28, 0x00000000},
  424. {0xe30, 0x1000dc1f}, {0xe34, 0x10008c1f},
  425. {0xe38, 0x02140102}, {0xe3c, 0x681604c2},
  426. {0xe40, 0x01007c00}, {0xe44, 0x01004800},
  427. {0xe48, 0xfb000000}, {0xe4c, 0x000028d1},
  428. {0xe50, 0x1000dc1f}, {0xe54, 0x10008c1f},
  429. {0xe58, 0x02140102}, {0xe5c, 0x28160d05},
  430. {0xe60, 0x00000008}, {0xe68, 0x001b25a4},
  431. {0xe6c, 0x631b25a0}, {0xe70, 0x631b25a0},
  432. {0xe74, 0x081b25a0}, {0xe78, 0x081b25a0},
  433. {0xe7c, 0x081b25a0}, {0xe80, 0x081b25a0},
  434. {0xe84, 0x631b25a0}, {0xe88, 0x081b25a0},
  435. {0xe8c, 0x631b25a0}, {0xed0, 0x631b25a0},
  436. {0xed4, 0x631b25a0}, {0xed8, 0x631b25a0},
  437. {0xedc, 0x001b25a0}, {0xee0, 0x001b25a0},
  438. {0xeec, 0x6b1b25a0}, {0xee8, 0x31555448},
  439. {0xf14, 0x00000003}, {0xf4c, 0x00000000},
  440. {0xf00, 0x00000300},
  441. {0xffff, 0xffffffff},
  442. };
  443. static struct rtl8xxxu_reg32val rtl8xxx_agc_standard_table[] = {
  444. {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
  445. {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
  446. {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
  447. {0xc78, 0x7a060001}, {0xc78, 0x79070001},
  448. {0xc78, 0x78080001}, {0xc78, 0x77090001},
  449. {0xc78, 0x760a0001}, {0xc78, 0x750b0001},
  450. {0xc78, 0x740c0001}, {0xc78, 0x730d0001},
  451. {0xc78, 0x720e0001}, {0xc78, 0x710f0001},
  452. {0xc78, 0x70100001}, {0xc78, 0x6f110001},
  453. {0xc78, 0x6e120001}, {0xc78, 0x6d130001},
  454. {0xc78, 0x6c140001}, {0xc78, 0x6b150001},
  455. {0xc78, 0x6a160001}, {0xc78, 0x69170001},
  456. {0xc78, 0x68180001}, {0xc78, 0x67190001},
  457. {0xc78, 0x661a0001}, {0xc78, 0x651b0001},
  458. {0xc78, 0x641c0001}, {0xc78, 0x631d0001},
  459. {0xc78, 0x621e0001}, {0xc78, 0x611f0001},
  460. {0xc78, 0x60200001}, {0xc78, 0x49210001},
  461. {0xc78, 0x48220001}, {0xc78, 0x47230001},
  462. {0xc78, 0x46240001}, {0xc78, 0x45250001},
  463. {0xc78, 0x44260001}, {0xc78, 0x43270001},
  464. {0xc78, 0x42280001}, {0xc78, 0x41290001},
  465. {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
  466. {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
  467. {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
  468. {0xc78, 0x21300001}, {0xc78, 0x20310001},
  469. {0xc78, 0x06320001}, {0xc78, 0x05330001},
  470. {0xc78, 0x04340001}, {0xc78, 0x03350001},
  471. {0xc78, 0x02360001}, {0xc78, 0x01370001},
  472. {0xc78, 0x00380001}, {0xc78, 0x00390001},
  473. {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
  474. {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
  475. {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
  476. {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
  477. {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
  478. {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
  479. {0xc78, 0x7a460001}, {0xc78, 0x79470001},
  480. {0xc78, 0x78480001}, {0xc78, 0x77490001},
  481. {0xc78, 0x764a0001}, {0xc78, 0x754b0001},
  482. {0xc78, 0x744c0001}, {0xc78, 0x734d0001},
  483. {0xc78, 0x724e0001}, {0xc78, 0x714f0001},
  484. {0xc78, 0x70500001}, {0xc78, 0x6f510001},
  485. {0xc78, 0x6e520001}, {0xc78, 0x6d530001},
  486. {0xc78, 0x6c540001}, {0xc78, 0x6b550001},
  487. {0xc78, 0x6a560001}, {0xc78, 0x69570001},
  488. {0xc78, 0x68580001}, {0xc78, 0x67590001},
  489. {0xc78, 0x665a0001}, {0xc78, 0x655b0001},
  490. {0xc78, 0x645c0001}, {0xc78, 0x635d0001},
  491. {0xc78, 0x625e0001}, {0xc78, 0x615f0001},
  492. {0xc78, 0x60600001}, {0xc78, 0x49610001},
  493. {0xc78, 0x48620001}, {0xc78, 0x47630001},
  494. {0xc78, 0x46640001}, {0xc78, 0x45650001},
  495. {0xc78, 0x44660001}, {0xc78, 0x43670001},
  496. {0xc78, 0x42680001}, {0xc78, 0x41690001},
  497. {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
  498. {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
  499. {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
  500. {0xc78, 0x21700001}, {0xc78, 0x20710001},
  501. {0xc78, 0x06720001}, {0xc78, 0x05730001},
  502. {0xc78, 0x04740001}, {0xc78, 0x03750001},
  503. {0xc78, 0x02760001}, {0xc78, 0x01770001},
  504. {0xc78, 0x00780001}, {0xc78, 0x00790001},
  505. {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
  506. {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
  507. {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
  508. {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
  509. {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
  510. {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
  511. {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
  512. {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
  513. {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
  514. {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
  515. {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
  516. {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
  517. {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
  518. {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
  519. {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
  520. {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
  521. {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
  522. {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
  523. {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
  524. {0xffff, 0xffffffff}
  525. };
  526. static struct rtl8xxxu_reg32val rtl8xxx_agc_highpa_table[] = {
  527. {0xc78, 0x7b000001}, {0xc78, 0x7b010001},
  528. {0xc78, 0x7b020001}, {0xc78, 0x7b030001},
  529. {0xc78, 0x7b040001}, {0xc78, 0x7b050001},
  530. {0xc78, 0x7b060001}, {0xc78, 0x7b070001},
  531. {0xc78, 0x7b080001}, {0xc78, 0x7a090001},
  532. {0xc78, 0x790a0001}, {0xc78, 0x780b0001},
  533. {0xc78, 0x770c0001}, {0xc78, 0x760d0001},
  534. {0xc78, 0x750e0001}, {0xc78, 0x740f0001},
  535. {0xc78, 0x73100001}, {0xc78, 0x72110001},
  536. {0xc78, 0x71120001}, {0xc78, 0x70130001},
  537. {0xc78, 0x6f140001}, {0xc78, 0x6e150001},
  538. {0xc78, 0x6d160001}, {0xc78, 0x6c170001},
  539. {0xc78, 0x6b180001}, {0xc78, 0x6a190001},
  540. {0xc78, 0x691a0001}, {0xc78, 0x681b0001},
  541. {0xc78, 0x671c0001}, {0xc78, 0x661d0001},
  542. {0xc78, 0x651e0001}, {0xc78, 0x641f0001},
  543. {0xc78, 0x63200001}, {0xc78, 0x62210001},
  544. {0xc78, 0x61220001}, {0xc78, 0x60230001},
  545. {0xc78, 0x46240001}, {0xc78, 0x45250001},
  546. {0xc78, 0x44260001}, {0xc78, 0x43270001},
  547. {0xc78, 0x42280001}, {0xc78, 0x41290001},
  548. {0xc78, 0x402a0001}, {0xc78, 0x262b0001},
  549. {0xc78, 0x252c0001}, {0xc78, 0x242d0001},
  550. {0xc78, 0x232e0001}, {0xc78, 0x222f0001},
  551. {0xc78, 0x21300001}, {0xc78, 0x20310001},
  552. {0xc78, 0x06320001}, {0xc78, 0x05330001},
  553. {0xc78, 0x04340001}, {0xc78, 0x03350001},
  554. {0xc78, 0x02360001}, {0xc78, 0x01370001},
  555. {0xc78, 0x00380001}, {0xc78, 0x00390001},
  556. {0xc78, 0x003a0001}, {0xc78, 0x003b0001},
  557. {0xc78, 0x003c0001}, {0xc78, 0x003d0001},
  558. {0xc78, 0x003e0001}, {0xc78, 0x003f0001},
  559. {0xc78, 0x7b400001}, {0xc78, 0x7b410001},
  560. {0xc78, 0x7b420001}, {0xc78, 0x7b430001},
  561. {0xc78, 0x7b440001}, {0xc78, 0x7b450001},
  562. {0xc78, 0x7b460001}, {0xc78, 0x7b470001},
  563. {0xc78, 0x7b480001}, {0xc78, 0x7a490001},
  564. {0xc78, 0x794a0001}, {0xc78, 0x784b0001},
  565. {0xc78, 0x774c0001}, {0xc78, 0x764d0001},
  566. {0xc78, 0x754e0001}, {0xc78, 0x744f0001},
  567. {0xc78, 0x73500001}, {0xc78, 0x72510001},
  568. {0xc78, 0x71520001}, {0xc78, 0x70530001},
  569. {0xc78, 0x6f540001}, {0xc78, 0x6e550001},
  570. {0xc78, 0x6d560001}, {0xc78, 0x6c570001},
  571. {0xc78, 0x6b580001}, {0xc78, 0x6a590001},
  572. {0xc78, 0x695a0001}, {0xc78, 0x685b0001},
  573. {0xc78, 0x675c0001}, {0xc78, 0x665d0001},
  574. {0xc78, 0x655e0001}, {0xc78, 0x645f0001},
  575. {0xc78, 0x63600001}, {0xc78, 0x62610001},
  576. {0xc78, 0x61620001}, {0xc78, 0x60630001},
  577. {0xc78, 0x46640001}, {0xc78, 0x45650001},
  578. {0xc78, 0x44660001}, {0xc78, 0x43670001},
  579. {0xc78, 0x42680001}, {0xc78, 0x41690001},
  580. {0xc78, 0x406a0001}, {0xc78, 0x266b0001},
  581. {0xc78, 0x256c0001}, {0xc78, 0x246d0001},
  582. {0xc78, 0x236e0001}, {0xc78, 0x226f0001},
  583. {0xc78, 0x21700001}, {0xc78, 0x20710001},
  584. {0xc78, 0x06720001}, {0xc78, 0x05730001},
  585. {0xc78, 0x04740001}, {0xc78, 0x03750001},
  586. {0xc78, 0x02760001}, {0xc78, 0x01770001},
  587. {0xc78, 0x00780001}, {0xc78, 0x00790001},
  588. {0xc78, 0x007a0001}, {0xc78, 0x007b0001},
  589. {0xc78, 0x007c0001}, {0xc78, 0x007d0001},
  590. {0xc78, 0x007e0001}, {0xc78, 0x007f0001},
  591. {0xc78, 0x3800001e}, {0xc78, 0x3801001e},
  592. {0xc78, 0x3802001e}, {0xc78, 0x3803001e},
  593. {0xc78, 0x3804001e}, {0xc78, 0x3805001e},
  594. {0xc78, 0x3806001e}, {0xc78, 0x3807001e},
  595. {0xc78, 0x3808001e}, {0xc78, 0x3c09001e},
  596. {0xc78, 0x3e0a001e}, {0xc78, 0x400b001e},
  597. {0xc78, 0x440c001e}, {0xc78, 0x480d001e},
  598. {0xc78, 0x4c0e001e}, {0xc78, 0x500f001e},
  599. {0xc78, 0x5210001e}, {0xc78, 0x5611001e},
  600. {0xc78, 0x5a12001e}, {0xc78, 0x5e13001e},
  601. {0xc78, 0x6014001e}, {0xc78, 0x6015001e},
  602. {0xc78, 0x6016001e}, {0xc78, 0x6217001e},
  603. {0xc78, 0x6218001e}, {0xc78, 0x6219001e},
  604. {0xc78, 0x621a001e}, {0xc78, 0x621b001e},
  605. {0xc78, 0x621c001e}, {0xc78, 0x621d001e},
  606. {0xc78, 0x621e001e}, {0xc78, 0x621f001e},
  607. {0xffff, 0xffffffff}
  608. };
  609. static struct rtl8xxxu_rfregs rtl8xxxu_rfregs[] = {
  610. { /* RF_A */
  611. .hssiparm1 = REG_FPGA0_XA_HSSI_PARM1,
  612. .hssiparm2 = REG_FPGA0_XA_HSSI_PARM2,
  613. .lssiparm = REG_FPGA0_XA_LSSI_PARM,
  614. .hspiread = REG_HSPI_XA_READBACK,
  615. .lssiread = REG_FPGA0_XA_LSSI_READBACK,
  616. .rf_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL,
  617. },
  618. { /* RF_B */
  619. .hssiparm1 = REG_FPGA0_XB_HSSI_PARM1,
  620. .hssiparm2 = REG_FPGA0_XB_HSSI_PARM2,
  621. .lssiparm = REG_FPGA0_XB_LSSI_PARM,
  622. .hspiread = REG_HSPI_XB_READBACK,
  623. .lssiread = REG_FPGA0_XB_LSSI_READBACK,
  624. .rf_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL,
  625. },
  626. };
  627. const u32 rtl8xxxu_iqk_phy_iq_bb_reg[RTL8XXXU_BB_REGS] = {
  628. REG_OFDM0_XA_RX_IQ_IMBALANCE,
  629. REG_OFDM0_XB_RX_IQ_IMBALANCE,
  630. REG_OFDM0_ENERGY_CCA_THRES,
  631. REG_OFDM0_AGCR_SSI_TABLE,
  632. REG_OFDM0_XA_TX_IQ_IMBALANCE,
  633. REG_OFDM0_XB_TX_IQ_IMBALANCE,
  634. REG_OFDM0_XC_TX_AFE,
  635. REG_OFDM0_XD_TX_AFE,
  636. REG_OFDM0_RX_IQ_EXT_ANTA
  637. };
  638. u8 rtl8xxxu_read8(struct rtl8xxxu_priv *priv, u16 addr)
  639. {
  640. struct usb_device *udev = priv->udev;
  641. int len;
  642. u8 data;
  643. mutex_lock(&priv->usb_buf_mutex);
  644. len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  645. REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
  646. addr, 0, &priv->usb_buf.val8, sizeof(u8),
  647. RTW_USB_CONTROL_MSG_TIMEOUT);
  648. data = priv->usb_buf.val8;
  649. mutex_unlock(&priv->usb_buf_mutex);
  650. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
  651. dev_info(&udev->dev, "%s(%04x) = 0x%02x, len %i\n",
  652. __func__, addr, data, len);
  653. return data;
  654. }
  655. u16 rtl8xxxu_read16(struct rtl8xxxu_priv *priv, u16 addr)
  656. {
  657. struct usb_device *udev = priv->udev;
  658. int len;
  659. u16 data;
  660. mutex_lock(&priv->usb_buf_mutex);
  661. len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  662. REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
  663. addr, 0, &priv->usb_buf.val16, sizeof(u16),
  664. RTW_USB_CONTROL_MSG_TIMEOUT);
  665. data = le16_to_cpu(priv->usb_buf.val16);
  666. mutex_unlock(&priv->usb_buf_mutex);
  667. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
  668. dev_info(&udev->dev, "%s(%04x) = 0x%04x, len %i\n",
  669. __func__, addr, data, len);
  670. return data;
  671. }
  672. u32 rtl8xxxu_read32(struct rtl8xxxu_priv *priv, u16 addr)
  673. {
  674. struct usb_device *udev = priv->udev;
  675. int len;
  676. u32 data;
  677. mutex_lock(&priv->usb_buf_mutex);
  678. len = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  679. REALTEK_USB_CMD_REQ, REALTEK_USB_READ,
  680. addr, 0, &priv->usb_buf.val32, sizeof(u32),
  681. RTW_USB_CONTROL_MSG_TIMEOUT);
  682. data = le32_to_cpu(priv->usb_buf.val32);
  683. mutex_unlock(&priv->usb_buf_mutex);
  684. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
  685. dev_info(&udev->dev, "%s(%04x) = 0x%08x, len %i\n",
  686. __func__, addr, data, len);
  687. return data;
  688. }
  689. int rtl8xxxu_write8(struct rtl8xxxu_priv *priv, u16 addr, u8 val)
  690. {
  691. struct usb_device *udev = priv->udev;
  692. int ret;
  693. mutex_lock(&priv->usb_buf_mutex);
  694. priv->usb_buf.val8 = val;
  695. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  696. REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
  697. addr, 0, &priv->usb_buf.val8, sizeof(u8),
  698. RTW_USB_CONTROL_MSG_TIMEOUT);
  699. mutex_unlock(&priv->usb_buf_mutex);
  700. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
  701. dev_info(&udev->dev, "%s(%04x) = 0x%02x\n",
  702. __func__, addr, val);
  703. return ret;
  704. }
  705. int rtl8xxxu_write16(struct rtl8xxxu_priv *priv, u16 addr, u16 val)
  706. {
  707. struct usb_device *udev = priv->udev;
  708. int ret;
  709. mutex_lock(&priv->usb_buf_mutex);
  710. priv->usb_buf.val16 = cpu_to_le16(val);
  711. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  712. REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
  713. addr, 0, &priv->usb_buf.val16, sizeof(u16),
  714. RTW_USB_CONTROL_MSG_TIMEOUT);
  715. mutex_unlock(&priv->usb_buf_mutex);
  716. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
  717. dev_info(&udev->dev, "%s(%04x) = 0x%04x\n",
  718. __func__, addr, val);
  719. return ret;
  720. }
  721. int rtl8xxxu_write32(struct rtl8xxxu_priv *priv, u16 addr, u32 val)
  722. {
  723. struct usb_device *udev = priv->udev;
  724. int ret;
  725. mutex_lock(&priv->usb_buf_mutex);
  726. priv->usb_buf.val32 = cpu_to_le32(val);
  727. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  728. REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
  729. addr, 0, &priv->usb_buf.val32, sizeof(u32),
  730. RTW_USB_CONTROL_MSG_TIMEOUT);
  731. mutex_unlock(&priv->usb_buf_mutex);
  732. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
  733. dev_info(&udev->dev, "%s(%04x) = 0x%08x\n",
  734. __func__, addr, val);
  735. return ret;
  736. }
  737. static int
  738. rtl8xxxu_writeN(struct rtl8xxxu_priv *priv, u16 addr, u8 *buf, u16 len)
  739. {
  740. struct usb_device *udev = priv->udev;
  741. int blocksize = priv->fops->writeN_block_size;
  742. int ret, i, count, remainder;
  743. count = len / blocksize;
  744. remainder = len % blocksize;
  745. for (i = 0; i < count; i++) {
  746. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  747. REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
  748. addr, 0, buf, blocksize,
  749. RTW_USB_CONTROL_MSG_TIMEOUT);
  750. if (ret != blocksize)
  751. goto write_error;
  752. addr += blocksize;
  753. buf += blocksize;
  754. }
  755. if (remainder) {
  756. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  757. REALTEK_USB_CMD_REQ, REALTEK_USB_WRITE,
  758. addr, 0, buf, remainder,
  759. RTW_USB_CONTROL_MSG_TIMEOUT);
  760. if (ret != remainder)
  761. goto write_error;
  762. }
  763. return len;
  764. write_error:
  765. dev_info(&udev->dev,
  766. "%s: Failed to write block at addr: %04x size: %04x\n",
  767. __func__, addr, blocksize);
  768. return -EAGAIN;
  769. }
  770. u32 rtl8xxxu_read_rfreg(struct rtl8xxxu_priv *priv,
  771. enum rtl8xxxu_rfpath path, u8 reg)
  772. {
  773. u32 hssia, val32, retval;
  774. hssia = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM2);
  775. if (path != RF_A)
  776. val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm2);
  777. else
  778. val32 = hssia;
  779. val32 &= ~FPGA0_HSSI_PARM2_ADDR_MASK;
  780. val32 |= (reg << FPGA0_HSSI_PARM2_ADDR_SHIFT);
  781. val32 |= FPGA0_HSSI_PARM2_EDGE_READ;
  782. hssia &= ~FPGA0_HSSI_PARM2_EDGE_READ;
  783. rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
  784. udelay(10);
  785. rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].hssiparm2, val32);
  786. udelay(100);
  787. hssia |= FPGA0_HSSI_PARM2_EDGE_READ;
  788. rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM2, hssia);
  789. udelay(10);
  790. val32 = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hssiparm1);
  791. if (val32 & FPGA0_HSSI_PARM1_PI)
  792. retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].hspiread);
  793. else
  794. retval = rtl8xxxu_read32(priv, rtl8xxxu_rfregs[path].lssiread);
  795. retval &= 0xfffff;
  796. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_READ)
  797. dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
  798. __func__, reg, retval);
  799. return retval;
  800. }
  801. /*
  802. * The RTL8723BU driver indicates that registers 0xb2 and 0xb6 can
  803. * have write issues in high temperature conditions. We may have to
  804. * retry writing them.
  805. */
  806. int rtl8xxxu_write_rfreg(struct rtl8xxxu_priv *priv,
  807. enum rtl8xxxu_rfpath path, u8 reg, u32 data)
  808. {
  809. int ret, retval;
  810. u32 dataaddr, val32;
  811. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_RFREG_WRITE)
  812. dev_info(&priv->udev->dev, "%s(%02x) = 0x%06x\n",
  813. __func__, reg, data);
  814. data &= FPGA0_LSSI_PARM_DATA_MASK;
  815. dataaddr = (reg << FPGA0_LSSI_PARM_ADDR_SHIFT) | data;
  816. if (priv->rtl_chip == RTL8192E) {
  817. val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
  818. val32 &= ~0x20000;
  819. rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
  820. }
  821. /* Use XB for path B */
  822. ret = rtl8xxxu_write32(priv, rtl8xxxu_rfregs[path].lssiparm, dataaddr);
  823. if (ret != sizeof(dataaddr))
  824. retval = -EIO;
  825. else
  826. retval = 0;
  827. udelay(1);
  828. if (priv->rtl_chip == RTL8192E) {
  829. val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
  830. val32 |= 0x20000;
  831. rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
  832. }
  833. return retval;
  834. }
  835. static int
  836. rtl8xxxu_gen1_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
  837. {
  838. struct device *dev = &priv->udev->dev;
  839. int mbox_nr, retry, retval = 0;
  840. int mbox_reg, mbox_ext_reg;
  841. u8 val8;
  842. mutex_lock(&priv->h2c_mutex);
  843. mbox_nr = priv->next_mbox;
  844. mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
  845. mbox_ext_reg = REG_HMBOX_EXT_0 + (mbox_nr * 2);
  846. /*
  847. * MBOX ready?
  848. */
  849. retry = 100;
  850. do {
  851. val8 = rtl8xxxu_read8(priv, REG_HMTFR);
  852. if (!(val8 & BIT(mbox_nr)))
  853. break;
  854. } while (retry--);
  855. if (!retry) {
  856. dev_info(dev, "%s: Mailbox busy\n", __func__);
  857. retval = -EBUSY;
  858. goto error;
  859. }
  860. /*
  861. * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
  862. */
  863. if (len > sizeof(u32)) {
  864. rtl8xxxu_write16(priv, mbox_ext_reg, le16_to_cpu(h2c->raw.ext));
  865. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
  866. dev_info(dev, "H2C_EXT %04x\n",
  867. le16_to_cpu(h2c->raw.ext));
  868. }
  869. rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
  870. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
  871. dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
  872. priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
  873. error:
  874. mutex_unlock(&priv->h2c_mutex);
  875. return retval;
  876. }
  877. int
  878. rtl8xxxu_gen2_h2c_cmd(struct rtl8xxxu_priv *priv, struct h2c_cmd *h2c, int len)
  879. {
  880. struct device *dev = &priv->udev->dev;
  881. int mbox_nr, retry, retval = 0;
  882. int mbox_reg, mbox_ext_reg;
  883. u8 val8;
  884. mutex_lock(&priv->h2c_mutex);
  885. mbox_nr = priv->next_mbox;
  886. mbox_reg = REG_HMBOX_0 + (mbox_nr * 4);
  887. mbox_ext_reg = REG_HMBOX_EXT0_8723B + (mbox_nr * 4);
  888. /*
  889. * MBOX ready?
  890. */
  891. retry = 100;
  892. do {
  893. val8 = rtl8xxxu_read8(priv, REG_HMTFR);
  894. if (!(val8 & BIT(mbox_nr)))
  895. break;
  896. } while (retry--);
  897. if (!retry) {
  898. dev_info(dev, "%s: Mailbox busy\n", __func__);
  899. retval = -EBUSY;
  900. goto error;
  901. }
  902. /*
  903. * Need to swap as it's being swapped again by rtl8xxxu_write16/32()
  904. */
  905. if (len > sizeof(u32)) {
  906. rtl8xxxu_write32(priv, mbox_ext_reg,
  907. le32_to_cpu(h2c->raw_wide.ext));
  908. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
  909. dev_info(dev, "H2C_EXT %08x\n",
  910. le32_to_cpu(h2c->raw_wide.ext));
  911. }
  912. rtl8xxxu_write32(priv, mbox_reg, le32_to_cpu(h2c->raw.data));
  913. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_H2C)
  914. dev_info(dev, "H2C %08x\n", le32_to_cpu(h2c->raw.data));
  915. priv->next_mbox = (mbox_nr + 1) % H2C_MAX_MBOX;
  916. error:
  917. mutex_unlock(&priv->h2c_mutex);
  918. return retval;
  919. }
  920. void rtl8xxxu_gen1_enable_rf(struct rtl8xxxu_priv *priv)
  921. {
  922. u8 val8;
  923. u32 val32;
  924. val8 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
  925. val8 |= BIT(0) | BIT(3);
  926. rtl8xxxu_write8(priv, REG_SPS0_CTRL, val8);
  927. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
  928. val32 &= ~(BIT(4) | BIT(5));
  929. val32 |= BIT(3);
  930. if (priv->rf_paths == 2) {
  931. val32 &= ~(BIT(20) | BIT(21));
  932. val32 |= BIT(19);
  933. }
  934. rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
  935. val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
  936. val32 &= ~OFDM_RF_PATH_TX_MASK;
  937. if (priv->tx_paths == 2)
  938. val32 |= OFDM_RF_PATH_TX_A | OFDM_RF_PATH_TX_B;
  939. else if (priv->rtl_chip == RTL8192C || priv->rtl_chip == RTL8191C)
  940. val32 |= OFDM_RF_PATH_TX_B;
  941. else
  942. val32 |= OFDM_RF_PATH_TX_A;
  943. rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
  944. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  945. val32 &= ~FPGA_RF_MODE_JAPAN;
  946. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  947. if (priv->rf_paths == 2)
  948. rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x63db25a0);
  949. else
  950. rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x631b25a0);
  951. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0x32d95);
  952. if (priv->rf_paths == 2)
  953. rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0x32d95);
  954. rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
  955. }
  956. void rtl8xxxu_gen1_disable_rf(struct rtl8xxxu_priv *priv)
  957. {
  958. u8 sps0;
  959. u32 val32;
  960. sps0 = rtl8xxxu_read8(priv, REG_SPS0_CTRL);
  961. /* RF RX code for preamble power saving */
  962. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_PARM);
  963. val32 &= ~(BIT(3) | BIT(4) | BIT(5));
  964. if (priv->rf_paths == 2)
  965. val32 &= ~(BIT(19) | BIT(20) | BIT(21));
  966. rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_PARM, val32);
  967. /* Disable TX for four paths */
  968. val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
  969. val32 &= ~OFDM_RF_PATH_TX_MASK;
  970. rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
  971. /* Enable power saving */
  972. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  973. val32 |= FPGA_RF_MODE_JAPAN;
  974. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  975. /* AFE control register to power down bits [30:22] */
  976. if (priv->rf_paths == 2)
  977. rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x00db25a0);
  978. else
  979. rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, 0x001b25a0);
  980. /* Power down RF module */
  981. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, 0);
  982. if (priv->rf_paths == 2)
  983. rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC, 0);
  984. sps0 &= ~(BIT(0) | BIT(3));
  985. rtl8xxxu_write8(priv, REG_SPS0_CTRL, sps0);
  986. }
  987. static void rtl8xxxu_stop_tx_beacon(struct rtl8xxxu_priv *priv)
  988. {
  989. u8 val8;
  990. val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL + 2);
  991. val8 &= ~BIT(6);
  992. rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL + 2, val8);
  993. rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 1, 0x64);
  994. val8 = rtl8xxxu_read8(priv, REG_TBTT_PROHIBIT + 2);
  995. val8 &= ~BIT(0);
  996. rtl8xxxu_write8(priv, REG_TBTT_PROHIBIT + 2, val8);
  997. }
  998. /*
  999. * The rtl8723a has 3 channel groups for it's efuse settings. It only
  1000. * supports the 2.4GHz band, so channels 1 - 14:
  1001. * group 0: channels 1 - 3
  1002. * group 1: channels 4 - 9
  1003. * group 2: channels 10 - 14
  1004. *
  1005. * Note: We index from 0 in the code
  1006. */
  1007. static int rtl8xxxu_gen1_channel_to_group(int channel)
  1008. {
  1009. int group;
  1010. if (channel < 4)
  1011. group = 0;
  1012. else if (channel < 10)
  1013. group = 1;
  1014. else
  1015. group = 2;
  1016. return group;
  1017. }
  1018. /*
  1019. * Valid for rtl8723bu and rtl8192eu
  1020. */
  1021. int rtl8xxxu_gen2_channel_to_group(int channel)
  1022. {
  1023. int group;
  1024. if (channel < 3)
  1025. group = 0;
  1026. else if (channel < 6)
  1027. group = 1;
  1028. else if (channel < 9)
  1029. group = 2;
  1030. else if (channel < 12)
  1031. group = 3;
  1032. else
  1033. group = 4;
  1034. return group;
  1035. }
  1036. void rtl8xxxu_gen1_config_channel(struct ieee80211_hw *hw)
  1037. {
  1038. struct rtl8xxxu_priv *priv = hw->priv;
  1039. u32 val32, rsr;
  1040. u8 val8, opmode;
  1041. bool ht = true;
  1042. int sec_ch_above, channel;
  1043. int i;
  1044. opmode = rtl8xxxu_read8(priv, REG_BW_OPMODE);
  1045. rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
  1046. channel = hw->conf.chandef.chan->hw_value;
  1047. switch (hw->conf.chandef.width) {
  1048. case NL80211_CHAN_WIDTH_20_NOHT:
  1049. ht = false;
  1050. case NL80211_CHAN_WIDTH_20:
  1051. opmode |= BW_OPMODE_20MHZ;
  1052. rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
  1053. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  1054. val32 &= ~FPGA_RF_MODE;
  1055. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  1056. val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
  1057. val32 &= ~FPGA_RF_MODE;
  1058. rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
  1059. val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
  1060. val32 |= FPGA0_ANALOG2_20MHZ;
  1061. rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
  1062. break;
  1063. case NL80211_CHAN_WIDTH_40:
  1064. if (hw->conf.chandef.center_freq1 >
  1065. hw->conf.chandef.chan->center_freq) {
  1066. sec_ch_above = 1;
  1067. channel += 2;
  1068. } else {
  1069. sec_ch_above = 0;
  1070. channel -= 2;
  1071. }
  1072. opmode &= ~BW_OPMODE_20MHZ;
  1073. rtl8xxxu_write8(priv, REG_BW_OPMODE, opmode);
  1074. rsr &= ~RSR_RSC_BANDWIDTH_40M;
  1075. if (sec_ch_above)
  1076. rsr |= RSR_RSC_UPPER_SUB_CHANNEL;
  1077. else
  1078. rsr |= RSR_RSC_LOWER_SUB_CHANNEL;
  1079. rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, rsr);
  1080. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  1081. val32 |= FPGA_RF_MODE;
  1082. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  1083. val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
  1084. val32 |= FPGA_RF_MODE;
  1085. rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
  1086. /*
  1087. * Set Control channel to upper or lower. These settings
  1088. * are required only for 40MHz
  1089. */
  1090. val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
  1091. val32 &= ~CCK0_SIDEBAND;
  1092. if (!sec_ch_above)
  1093. val32 |= CCK0_SIDEBAND;
  1094. rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
  1095. val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
  1096. val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
  1097. if (sec_ch_above)
  1098. val32 |= OFDM_LSTF_PRIME_CH_LOW;
  1099. else
  1100. val32 |= OFDM_LSTF_PRIME_CH_HIGH;
  1101. rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
  1102. val32 = rtl8xxxu_read32(priv, REG_FPGA0_ANALOG2);
  1103. val32 &= ~FPGA0_ANALOG2_20MHZ;
  1104. rtl8xxxu_write32(priv, REG_FPGA0_ANALOG2, val32);
  1105. val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
  1106. val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
  1107. if (sec_ch_above)
  1108. val32 |= FPGA0_PS_UPPER_CHANNEL;
  1109. else
  1110. val32 |= FPGA0_PS_LOWER_CHANNEL;
  1111. rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
  1112. break;
  1113. default:
  1114. break;
  1115. }
  1116. for (i = RF_A; i < priv->rf_paths; i++) {
  1117. val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
  1118. val32 &= ~MODE_AG_CHANNEL_MASK;
  1119. val32 |= channel;
  1120. rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
  1121. }
  1122. if (ht)
  1123. val8 = 0x0e;
  1124. else
  1125. val8 = 0x0a;
  1126. rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
  1127. rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
  1128. rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
  1129. rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
  1130. for (i = RF_A; i < priv->rf_paths; i++) {
  1131. val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
  1132. if (hw->conf.chandef.width == NL80211_CHAN_WIDTH_40)
  1133. val32 &= ~MODE_AG_CHANNEL_20MHZ;
  1134. else
  1135. val32 |= MODE_AG_CHANNEL_20MHZ;
  1136. rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
  1137. }
  1138. }
  1139. void rtl8xxxu_gen2_config_channel(struct ieee80211_hw *hw)
  1140. {
  1141. struct rtl8xxxu_priv *priv = hw->priv;
  1142. u32 val32, rsr;
  1143. u8 val8, subchannel;
  1144. u16 rf_mode_bw;
  1145. bool ht = true;
  1146. int sec_ch_above, channel;
  1147. int i;
  1148. rf_mode_bw = rtl8xxxu_read16(priv, REG_WMAC_TRXPTCL_CTL);
  1149. rf_mode_bw &= ~WMAC_TRXPTCL_CTL_BW_MASK;
  1150. rsr = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
  1151. channel = hw->conf.chandef.chan->hw_value;
  1152. /* Hack */
  1153. subchannel = 0;
  1154. switch (hw->conf.chandef.width) {
  1155. case NL80211_CHAN_WIDTH_20_NOHT:
  1156. ht = false;
  1157. case NL80211_CHAN_WIDTH_20:
  1158. rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_20;
  1159. subchannel = 0;
  1160. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  1161. val32 &= ~FPGA_RF_MODE;
  1162. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  1163. val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
  1164. val32 &= ~FPGA_RF_MODE;
  1165. rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
  1166. val32 = rtl8xxxu_read32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT);
  1167. val32 &= ~(BIT(30) | BIT(31));
  1168. rtl8xxxu_write32(priv, REG_OFDM0_TX_PSDO_NOISE_WEIGHT, val32);
  1169. break;
  1170. case NL80211_CHAN_WIDTH_40:
  1171. rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_40;
  1172. if (hw->conf.chandef.center_freq1 >
  1173. hw->conf.chandef.chan->center_freq) {
  1174. sec_ch_above = 1;
  1175. channel += 2;
  1176. } else {
  1177. sec_ch_above = 0;
  1178. channel -= 2;
  1179. }
  1180. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  1181. val32 |= FPGA_RF_MODE;
  1182. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  1183. val32 = rtl8xxxu_read32(priv, REG_FPGA1_RF_MODE);
  1184. val32 |= FPGA_RF_MODE;
  1185. rtl8xxxu_write32(priv, REG_FPGA1_RF_MODE, val32);
  1186. /*
  1187. * Set Control channel to upper or lower. These settings
  1188. * are required only for 40MHz
  1189. */
  1190. val32 = rtl8xxxu_read32(priv, REG_CCK0_SYSTEM);
  1191. val32 &= ~CCK0_SIDEBAND;
  1192. if (!sec_ch_above)
  1193. val32 |= CCK0_SIDEBAND;
  1194. rtl8xxxu_write32(priv, REG_CCK0_SYSTEM, val32);
  1195. val32 = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
  1196. val32 &= ~OFDM_LSTF_PRIME_CH_MASK; /* 0xc00 */
  1197. if (sec_ch_above)
  1198. val32 |= OFDM_LSTF_PRIME_CH_LOW;
  1199. else
  1200. val32 |= OFDM_LSTF_PRIME_CH_HIGH;
  1201. rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
  1202. val32 = rtl8xxxu_read32(priv, REG_FPGA0_POWER_SAVE);
  1203. val32 &= ~(FPGA0_PS_LOWER_CHANNEL | FPGA0_PS_UPPER_CHANNEL);
  1204. if (sec_ch_above)
  1205. val32 |= FPGA0_PS_UPPER_CHANNEL;
  1206. else
  1207. val32 |= FPGA0_PS_LOWER_CHANNEL;
  1208. rtl8xxxu_write32(priv, REG_FPGA0_POWER_SAVE, val32);
  1209. break;
  1210. case NL80211_CHAN_WIDTH_80:
  1211. rf_mode_bw |= WMAC_TRXPTCL_CTL_BW_80;
  1212. break;
  1213. default:
  1214. break;
  1215. }
  1216. for (i = RF_A; i < priv->rf_paths; i++) {
  1217. val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
  1218. val32 &= ~MODE_AG_CHANNEL_MASK;
  1219. val32 |= channel;
  1220. rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
  1221. }
  1222. rtl8xxxu_write16(priv, REG_WMAC_TRXPTCL_CTL, rf_mode_bw);
  1223. rtl8xxxu_write8(priv, REG_DATA_SUBCHANNEL, subchannel);
  1224. if (ht)
  1225. val8 = 0x0e;
  1226. else
  1227. val8 = 0x0a;
  1228. rtl8xxxu_write8(priv, REG_SIFS_CCK + 1, val8);
  1229. rtl8xxxu_write8(priv, REG_SIFS_OFDM + 1, val8);
  1230. rtl8xxxu_write16(priv, REG_R2T_SIFS, 0x0808);
  1231. rtl8xxxu_write16(priv, REG_T2T_SIFS, 0x0a0a);
  1232. for (i = RF_A; i < priv->rf_paths; i++) {
  1233. val32 = rtl8xxxu_read_rfreg(priv, i, RF6052_REG_MODE_AG);
  1234. val32 &= ~MODE_AG_BW_MASK;
  1235. switch(hw->conf.chandef.width) {
  1236. case NL80211_CHAN_WIDTH_80:
  1237. val32 |= MODE_AG_BW_80MHZ_8723B;
  1238. break;
  1239. case NL80211_CHAN_WIDTH_40:
  1240. val32 |= MODE_AG_BW_40MHZ_8723B;
  1241. break;
  1242. default:
  1243. val32 |= MODE_AG_BW_20MHZ_8723B;
  1244. break;
  1245. }
  1246. rtl8xxxu_write_rfreg(priv, i, RF6052_REG_MODE_AG, val32);
  1247. }
  1248. }
  1249. void
  1250. rtl8xxxu_gen1_set_tx_power(struct rtl8xxxu_priv *priv, int channel, bool ht40)
  1251. {
  1252. struct rtl8xxxu_power_base *power_base = priv->power_base;
  1253. u8 cck[RTL8723A_MAX_RF_PATHS], ofdm[RTL8723A_MAX_RF_PATHS];
  1254. u8 ofdmbase[RTL8723A_MAX_RF_PATHS], mcsbase[RTL8723A_MAX_RF_PATHS];
  1255. u32 val32, ofdm_a, ofdm_b, mcs_a, mcs_b;
  1256. u8 val8;
  1257. int group, i;
  1258. group = rtl8xxxu_gen1_channel_to_group(channel);
  1259. cck[0] = priv->cck_tx_power_index_A[group] - 1;
  1260. cck[1] = priv->cck_tx_power_index_B[group] - 1;
  1261. if (priv->hi_pa) {
  1262. if (cck[0] > 0x20)
  1263. cck[0] = 0x20;
  1264. if (cck[1] > 0x20)
  1265. cck[1] = 0x20;
  1266. }
  1267. ofdm[0] = priv->ht40_1s_tx_power_index_A[group];
  1268. ofdm[1] = priv->ht40_1s_tx_power_index_B[group];
  1269. if (ofdm[0])
  1270. ofdm[0] -= 1;
  1271. if (ofdm[1])
  1272. ofdm[1] -= 1;
  1273. ofdmbase[0] = ofdm[0] + priv->ofdm_tx_power_index_diff[group].a;
  1274. ofdmbase[1] = ofdm[1] + priv->ofdm_tx_power_index_diff[group].b;
  1275. mcsbase[0] = ofdm[0];
  1276. mcsbase[1] = ofdm[1];
  1277. if (!ht40) {
  1278. mcsbase[0] += priv->ht20_tx_power_index_diff[group].a;
  1279. mcsbase[1] += priv->ht20_tx_power_index_diff[group].b;
  1280. }
  1281. if (priv->tx_paths > 1) {
  1282. if (ofdm[0] > priv->ht40_2s_tx_power_index_diff[group].a)
  1283. ofdm[0] -= priv->ht40_2s_tx_power_index_diff[group].a;
  1284. if (ofdm[1] > priv->ht40_2s_tx_power_index_diff[group].b)
  1285. ofdm[1] -= priv->ht40_2s_tx_power_index_diff[group].b;
  1286. }
  1287. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
  1288. dev_info(&priv->udev->dev,
  1289. "%s: Setting TX power CCK A: %02x, "
  1290. "CCK B: %02x, OFDM A: %02x, OFDM B: %02x\n",
  1291. __func__, cck[0], cck[1], ofdm[0], ofdm[1]);
  1292. for (i = 0; i < RTL8723A_MAX_RF_PATHS; i++) {
  1293. if (cck[i] > RF6052_MAX_TX_PWR)
  1294. cck[i] = RF6052_MAX_TX_PWR;
  1295. if (ofdm[i] > RF6052_MAX_TX_PWR)
  1296. ofdm[i] = RF6052_MAX_TX_PWR;
  1297. }
  1298. val32 = rtl8xxxu_read32(priv, REG_TX_AGC_A_CCK1_MCS32);
  1299. val32 &= 0xffff00ff;
  1300. val32 |= (cck[0] << 8);
  1301. rtl8xxxu_write32(priv, REG_TX_AGC_A_CCK1_MCS32, val32);
  1302. val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
  1303. val32 &= 0xff;
  1304. val32 |= ((cck[0] << 8) | (cck[0] << 16) | (cck[0] << 24));
  1305. rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
  1306. val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11);
  1307. val32 &= 0xffffff00;
  1308. val32 |= cck[1];
  1309. rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK11_A_CCK2_11, val32);
  1310. val32 = rtl8xxxu_read32(priv, REG_TX_AGC_B_CCK1_55_MCS32);
  1311. val32 &= 0xff;
  1312. val32 |= ((cck[1] << 8) | (cck[1] << 16) | (cck[1] << 24));
  1313. rtl8xxxu_write32(priv, REG_TX_AGC_B_CCK1_55_MCS32, val32);
  1314. ofdm_a = ofdmbase[0] | ofdmbase[0] << 8 |
  1315. ofdmbase[0] << 16 | ofdmbase[0] << 24;
  1316. ofdm_b = ofdmbase[1] | ofdmbase[1] << 8 |
  1317. ofdmbase[1] << 16 | ofdmbase[1] << 24;
  1318. rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE18_06,
  1319. ofdm_a + power_base->reg_0e00);
  1320. rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE18_06,
  1321. ofdm_b + power_base->reg_0830);
  1322. rtl8xxxu_write32(priv, REG_TX_AGC_A_RATE54_24,
  1323. ofdm_a + power_base->reg_0e04);
  1324. rtl8xxxu_write32(priv, REG_TX_AGC_B_RATE54_24,
  1325. ofdm_b + power_base->reg_0834);
  1326. mcs_a = mcsbase[0] | mcsbase[0] << 8 |
  1327. mcsbase[0] << 16 | mcsbase[0] << 24;
  1328. mcs_b = mcsbase[1] | mcsbase[1] << 8 |
  1329. mcsbase[1] << 16 | mcsbase[1] << 24;
  1330. rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS03_MCS00,
  1331. mcs_a + power_base->reg_0e10);
  1332. rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS03_MCS00,
  1333. mcs_b + power_base->reg_083c);
  1334. rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS07_MCS04,
  1335. mcs_a + power_base->reg_0e14);
  1336. rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS07_MCS04,
  1337. mcs_b + power_base->reg_0848);
  1338. rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS11_MCS08,
  1339. mcs_a + power_base->reg_0e18);
  1340. rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS11_MCS08,
  1341. mcs_b + power_base->reg_084c);
  1342. rtl8xxxu_write32(priv, REG_TX_AGC_A_MCS15_MCS12,
  1343. mcs_a + power_base->reg_0e1c);
  1344. for (i = 0; i < 3; i++) {
  1345. if (i != 2)
  1346. val8 = (mcsbase[0] > 8) ? (mcsbase[0] - 8) : 0;
  1347. else
  1348. val8 = (mcsbase[0] > 6) ? (mcsbase[0] - 6) : 0;
  1349. rtl8xxxu_write8(priv, REG_OFDM0_XC_TX_IQ_IMBALANCE + i, val8);
  1350. }
  1351. rtl8xxxu_write32(priv, REG_TX_AGC_B_MCS15_MCS12,
  1352. mcs_b + power_base->reg_0868);
  1353. for (i = 0; i < 3; i++) {
  1354. if (i != 2)
  1355. val8 = (mcsbase[1] > 8) ? (mcsbase[1] - 8) : 0;
  1356. else
  1357. val8 = (mcsbase[1] > 6) ? (mcsbase[1] - 6) : 0;
  1358. rtl8xxxu_write8(priv, REG_OFDM0_XD_TX_IQ_IMBALANCE + i, val8);
  1359. }
  1360. }
  1361. static void rtl8xxxu_set_linktype(struct rtl8xxxu_priv *priv,
  1362. enum nl80211_iftype linktype)
  1363. {
  1364. u8 val8;
  1365. val8 = rtl8xxxu_read8(priv, REG_MSR);
  1366. val8 &= ~MSR_LINKTYPE_MASK;
  1367. switch (linktype) {
  1368. case NL80211_IFTYPE_UNSPECIFIED:
  1369. val8 |= MSR_LINKTYPE_NONE;
  1370. break;
  1371. case NL80211_IFTYPE_ADHOC:
  1372. val8 |= MSR_LINKTYPE_ADHOC;
  1373. break;
  1374. case NL80211_IFTYPE_STATION:
  1375. val8 |= MSR_LINKTYPE_STATION;
  1376. break;
  1377. case NL80211_IFTYPE_AP:
  1378. val8 |= MSR_LINKTYPE_AP;
  1379. break;
  1380. default:
  1381. goto out;
  1382. }
  1383. rtl8xxxu_write8(priv, REG_MSR, val8);
  1384. out:
  1385. return;
  1386. }
  1387. static void
  1388. rtl8xxxu_set_retry(struct rtl8xxxu_priv *priv, u16 short_retry, u16 long_retry)
  1389. {
  1390. u16 val16;
  1391. val16 = ((short_retry << RETRY_LIMIT_SHORT_SHIFT) &
  1392. RETRY_LIMIT_SHORT_MASK) |
  1393. ((long_retry << RETRY_LIMIT_LONG_SHIFT) &
  1394. RETRY_LIMIT_LONG_MASK);
  1395. rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
  1396. }
  1397. static void
  1398. rtl8xxxu_set_spec_sifs(struct rtl8xxxu_priv *priv, u16 cck, u16 ofdm)
  1399. {
  1400. u16 val16;
  1401. val16 = ((cck << SPEC_SIFS_CCK_SHIFT) & SPEC_SIFS_CCK_MASK) |
  1402. ((ofdm << SPEC_SIFS_OFDM_SHIFT) & SPEC_SIFS_OFDM_MASK);
  1403. rtl8xxxu_write16(priv, REG_SPEC_SIFS, val16);
  1404. }
  1405. static void rtl8xxxu_print_chipinfo(struct rtl8xxxu_priv *priv)
  1406. {
  1407. struct device *dev = &priv->udev->dev;
  1408. char *cut;
  1409. switch (priv->chip_cut) {
  1410. case 0:
  1411. cut = "A";
  1412. break;
  1413. case 1:
  1414. cut = "B";
  1415. break;
  1416. case 2:
  1417. cut = "C";
  1418. break;
  1419. case 3:
  1420. cut = "D";
  1421. break;
  1422. case 4:
  1423. cut = "E";
  1424. break;
  1425. default:
  1426. cut = "unknown";
  1427. }
  1428. dev_info(dev,
  1429. "RTL%s rev %s (%s) %iT%iR, TX queues %i, WiFi=%i, BT=%i, GPS=%i, HI PA=%i\n",
  1430. priv->chip_name, cut, priv->chip_vendor, priv->tx_paths,
  1431. priv->rx_paths, priv->ep_tx_count, priv->has_wifi,
  1432. priv->has_bluetooth, priv->has_gps, priv->hi_pa);
  1433. dev_info(dev, "RTL%s MAC: %pM\n", priv->chip_name, priv->mac_addr);
  1434. }
  1435. static int rtl8xxxu_identify_chip(struct rtl8xxxu_priv *priv)
  1436. {
  1437. struct device *dev = &priv->udev->dev;
  1438. u32 val32, bonding;
  1439. u16 val16;
  1440. val32 = rtl8xxxu_read32(priv, REG_SYS_CFG);
  1441. priv->chip_cut = (val32 & SYS_CFG_CHIP_VERSION_MASK) >>
  1442. SYS_CFG_CHIP_VERSION_SHIFT;
  1443. if (val32 & SYS_CFG_TRP_VAUX_EN) {
  1444. dev_info(dev, "Unsupported test chip\n");
  1445. return -ENOTSUPP;
  1446. }
  1447. if (val32 & SYS_CFG_BT_FUNC) {
  1448. if (priv->chip_cut >= 3) {
  1449. sprintf(priv->chip_name, "8723BU");
  1450. priv->rtl_chip = RTL8723B;
  1451. } else {
  1452. sprintf(priv->chip_name, "8723AU");
  1453. priv->usb_interrupts = 1;
  1454. priv->rtl_chip = RTL8723A;
  1455. }
  1456. priv->rf_paths = 1;
  1457. priv->rx_paths = 1;
  1458. priv->tx_paths = 1;
  1459. val32 = rtl8xxxu_read32(priv, REG_MULTI_FUNC_CTRL);
  1460. if (val32 & MULTI_WIFI_FUNC_EN)
  1461. priv->has_wifi = 1;
  1462. if (val32 & MULTI_BT_FUNC_EN)
  1463. priv->has_bluetooth = 1;
  1464. if (val32 & MULTI_GPS_FUNC_EN)
  1465. priv->has_gps = 1;
  1466. priv->is_multi_func = 1;
  1467. } else if (val32 & SYS_CFG_TYPE_ID) {
  1468. bonding = rtl8xxxu_read32(priv, REG_HPON_FSM);
  1469. bonding &= HPON_FSM_BONDING_MASK;
  1470. if (priv->fops->tx_desc_size ==
  1471. sizeof(struct rtl8xxxu_txdesc40)) {
  1472. if (bonding == HPON_FSM_BONDING_1T2R) {
  1473. sprintf(priv->chip_name, "8191EU");
  1474. priv->rf_paths = 2;
  1475. priv->rx_paths = 2;
  1476. priv->tx_paths = 1;
  1477. priv->rtl_chip = RTL8191E;
  1478. } else {
  1479. sprintf(priv->chip_name, "8192EU");
  1480. priv->rf_paths = 2;
  1481. priv->rx_paths = 2;
  1482. priv->tx_paths = 2;
  1483. priv->rtl_chip = RTL8192E;
  1484. }
  1485. } else if (bonding == HPON_FSM_BONDING_1T2R) {
  1486. sprintf(priv->chip_name, "8191CU");
  1487. priv->rf_paths = 2;
  1488. priv->rx_paths = 2;
  1489. priv->tx_paths = 1;
  1490. priv->usb_interrupts = 1;
  1491. priv->rtl_chip = RTL8191C;
  1492. } else {
  1493. sprintf(priv->chip_name, "8192CU");
  1494. priv->rf_paths = 2;
  1495. priv->rx_paths = 2;
  1496. priv->tx_paths = 2;
  1497. priv->usb_interrupts = 1;
  1498. priv->rtl_chip = RTL8192C;
  1499. }
  1500. priv->has_wifi = 1;
  1501. } else {
  1502. sprintf(priv->chip_name, "8188CU");
  1503. priv->rf_paths = 1;
  1504. priv->rx_paths = 1;
  1505. priv->tx_paths = 1;
  1506. priv->rtl_chip = RTL8188C;
  1507. priv->usb_interrupts = 1;
  1508. priv->has_wifi = 1;
  1509. }
  1510. switch (priv->rtl_chip) {
  1511. case RTL8188E:
  1512. case RTL8192E:
  1513. case RTL8723B:
  1514. switch (val32 & SYS_CFG_VENDOR_EXT_MASK) {
  1515. case SYS_CFG_VENDOR_ID_TSMC:
  1516. sprintf(priv->chip_vendor, "TSMC");
  1517. break;
  1518. case SYS_CFG_VENDOR_ID_SMIC:
  1519. sprintf(priv->chip_vendor, "SMIC");
  1520. priv->vendor_smic = 1;
  1521. break;
  1522. case SYS_CFG_VENDOR_ID_UMC:
  1523. sprintf(priv->chip_vendor, "UMC");
  1524. priv->vendor_umc = 1;
  1525. break;
  1526. default:
  1527. sprintf(priv->chip_vendor, "unknown");
  1528. }
  1529. break;
  1530. default:
  1531. if (val32 & SYS_CFG_VENDOR_ID) {
  1532. sprintf(priv->chip_vendor, "UMC");
  1533. priv->vendor_umc = 1;
  1534. } else {
  1535. sprintf(priv->chip_vendor, "TSMC");
  1536. }
  1537. }
  1538. val32 = rtl8xxxu_read32(priv, REG_GPIO_OUTSTS);
  1539. priv->rom_rev = (val32 & GPIO_RF_RL_ID) >> 28;
  1540. val16 = rtl8xxxu_read16(priv, REG_NORMAL_SIE_EP_TX);
  1541. if (val16 & NORMAL_SIE_EP_TX_HIGH_MASK) {
  1542. priv->ep_tx_high_queue = 1;
  1543. priv->ep_tx_count++;
  1544. }
  1545. if (val16 & NORMAL_SIE_EP_TX_NORMAL_MASK) {
  1546. priv->ep_tx_normal_queue = 1;
  1547. priv->ep_tx_count++;
  1548. }
  1549. if (val16 & NORMAL_SIE_EP_TX_LOW_MASK) {
  1550. priv->ep_tx_low_queue = 1;
  1551. priv->ep_tx_count++;
  1552. }
  1553. /*
  1554. * Fallback for devices that do not provide REG_NORMAL_SIE_EP_TX
  1555. */
  1556. if (!priv->ep_tx_count) {
  1557. switch (priv->nr_out_eps) {
  1558. case 4:
  1559. case 3:
  1560. priv->ep_tx_low_queue = 1;
  1561. priv->ep_tx_count++;
  1562. case 2:
  1563. priv->ep_tx_normal_queue = 1;
  1564. priv->ep_tx_count++;
  1565. case 1:
  1566. priv->ep_tx_high_queue = 1;
  1567. priv->ep_tx_count++;
  1568. break;
  1569. default:
  1570. dev_info(dev, "Unsupported USB TX end-points\n");
  1571. return -ENOTSUPP;
  1572. }
  1573. }
  1574. return 0;
  1575. }
  1576. static int
  1577. rtl8xxxu_read_efuse8(struct rtl8xxxu_priv *priv, u16 offset, u8 *data)
  1578. {
  1579. int i;
  1580. u8 val8;
  1581. u32 val32;
  1582. /* Write Address */
  1583. rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 1, offset & 0xff);
  1584. val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 2);
  1585. val8 &= 0xfc;
  1586. val8 |= (offset >> 8) & 0x03;
  1587. rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 2, val8);
  1588. val8 = rtl8xxxu_read8(priv, REG_EFUSE_CTRL + 3);
  1589. rtl8xxxu_write8(priv, REG_EFUSE_CTRL + 3, val8 & 0x7f);
  1590. /* Poll for data read */
  1591. val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
  1592. for (i = 0; i < RTL8XXXU_MAX_REG_POLL; i++) {
  1593. val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
  1594. if (val32 & BIT(31))
  1595. break;
  1596. }
  1597. if (i == RTL8XXXU_MAX_REG_POLL)
  1598. return -EIO;
  1599. udelay(50);
  1600. val32 = rtl8xxxu_read32(priv, REG_EFUSE_CTRL);
  1601. *data = val32 & 0xff;
  1602. return 0;
  1603. }
  1604. static int rtl8xxxu_read_efuse(struct rtl8xxxu_priv *priv)
  1605. {
  1606. struct device *dev = &priv->udev->dev;
  1607. int i, ret = 0;
  1608. u8 val8, word_mask, header, extheader;
  1609. u16 val16, efuse_addr, offset;
  1610. u32 val32;
  1611. val16 = rtl8xxxu_read16(priv, REG_9346CR);
  1612. if (val16 & EEPROM_ENABLE)
  1613. priv->has_eeprom = 1;
  1614. if (val16 & EEPROM_BOOT)
  1615. priv->boot_eeprom = 1;
  1616. if (priv->is_multi_func) {
  1617. val32 = rtl8xxxu_read32(priv, REG_EFUSE_TEST);
  1618. val32 = (val32 & ~EFUSE_SELECT_MASK) | EFUSE_WIFI_SELECT;
  1619. rtl8xxxu_write32(priv, REG_EFUSE_TEST, val32);
  1620. }
  1621. dev_dbg(dev, "Booting from %s\n",
  1622. priv->boot_eeprom ? "EEPROM" : "EFUSE");
  1623. rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_ENABLE);
  1624. /* 1.2V Power: From VDDON with Power Cut(0x0000[15]), default valid */
  1625. val16 = rtl8xxxu_read16(priv, REG_SYS_ISO_CTRL);
  1626. if (!(val16 & SYS_ISO_PWC_EV12V)) {
  1627. val16 |= SYS_ISO_PWC_EV12V;
  1628. rtl8xxxu_write16(priv, REG_SYS_ISO_CTRL, val16);
  1629. }
  1630. /* Reset: 0x0000[28], default valid */
  1631. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1632. if (!(val16 & SYS_FUNC_ELDR)) {
  1633. val16 |= SYS_FUNC_ELDR;
  1634. rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
  1635. }
  1636. /*
  1637. * Clock: Gated(0x0008[5]) 8M(0x0008[1]) clock from ANA, default valid
  1638. */
  1639. val16 = rtl8xxxu_read16(priv, REG_SYS_CLKR);
  1640. if (!(val16 & SYS_CLK_LOADER_ENABLE) || !(val16 & SYS_CLK_ANA8M)) {
  1641. val16 |= (SYS_CLK_LOADER_ENABLE | SYS_CLK_ANA8M);
  1642. rtl8xxxu_write16(priv, REG_SYS_CLKR, val16);
  1643. }
  1644. /* Default value is 0xff */
  1645. memset(priv->efuse_wifi.raw, 0xff, EFUSE_MAP_LEN);
  1646. efuse_addr = 0;
  1647. while (efuse_addr < EFUSE_REAL_CONTENT_LEN_8723A) {
  1648. u16 map_addr;
  1649. ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &header);
  1650. if (ret || header == 0xff)
  1651. goto exit;
  1652. if ((header & 0x1f) == 0x0f) { /* extended header */
  1653. offset = (header & 0xe0) >> 5;
  1654. ret = rtl8xxxu_read_efuse8(priv, efuse_addr++,
  1655. &extheader);
  1656. if (ret)
  1657. goto exit;
  1658. /* All words disabled */
  1659. if ((extheader & 0x0f) == 0x0f)
  1660. continue;
  1661. offset |= ((extheader & 0xf0) >> 1);
  1662. word_mask = extheader & 0x0f;
  1663. } else {
  1664. offset = (header >> 4) & 0x0f;
  1665. word_mask = header & 0x0f;
  1666. }
  1667. /* Get word enable value from PG header */
  1668. /* We have 8 bits to indicate validity */
  1669. map_addr = offset * 8;
  1670. if (map_addr >= EFUSE_MAP_LEN) {
  1671. dev_warn(dev, "%s: Illegal map_addr (%04x), "
  1672. "efuse corrupt!\n",
  1673. __func__, map_addr);
  1674. ret = -EINVAL;
  1675. goto exit;
  1676. }
  1677. for (i = 0; i < EFUSE_MAX_WORD_UNIT; i++) {
  1678. /* Check word enable condition in the section */
  1679. if (word_mask & BIT(i)) {
  1680. map_addr += 2;
  1681. continue;
  1682. }
  1683. ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
  1684. if (ret)
  1685. goto exit;
  1686. priv->efuse_wifi.raw[map_addr++] = val8;
  1687. ret = rtl8xxxu_read_efuse8(priv, efuse_addr++, &val8);
  1688. if (ret)
  1689. goto exit;
  1690. priv->efuse_wifi.raw[map_addr++] = val8;
  1691. }
  1692. }
  1693. exit:
  1694. rtl8xxxu_write8(priv, REG_EFUSE_ACCESS, EFUSE_ACCESS_DISABLE);
  1695. return ret;
  1696. }
  1697. void rtl8xxxu_reset_8051(struct rtl8xxxu_priv *priv)
  1698. {
  1699. u8 val8;
  1700. u16 sys_func;
  1701. val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
  1702. val8 &= ~BIT(0);
  1703. rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
  1704. sys_func = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1705. sys_func &= ~SYS_FUNC_CPU_ENABLE;
  1706. rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
  1707. val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
  1708. val8 |= BIT(0);
  1709. rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
  1710. sys_func |= SYS_FUNC_CPU_ENABLE;
  1711. rtl8xxxu_write16(priv, REG_SYS_FUNC, sys_func);
  1712. }
  1713. static int rtl8xxxu_start_firmware(struct rtl8xxxu_priv *priv)
  1714. {
  1715. struct device *dev = &priv->udev->dev;
  1716. int ret = 0, i;
  1717. u32 val32;
  1718. /* Poll checksum report */
  1719. for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
  1720. val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
  1721. if (val32 & MCU_FW_DL_CSUM_REPORT)
  1722. break;
  1723. }
  1724. if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
  1725. dev_warn(dev, "Firmware checksum poll timed out\n");
  1726. ret = -EAGAIN;
  1727. goto exit;
  1728. }
  1729. val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
  1730. val32 |= MCU_FW_DL_READY;
  1731. val32 &= ~MCU_WINT_INIT_READY;
  1732. rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
  1733. /*
  1734. * Reset the 8051 in order for the firmware to start running,
  1735. * otherwise it won't come up on the 8192eu
  1736. */
  1737. priv->fops->reset_8051(priv);
  1738. /* Wait for firmware to become ready */
  1739. for (i = 0; i < RTL8XXXU_FIRMWARE_POLL_MAX; i++) {
  1740. val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
  1741. if (val32 & MCU_WINT_INIT_READY)
  1742. break;
  1743. udelay(100);
  1744. }
  1745. if (i == RTL8XXXU_FIRMWARE_POLL_MAX) {
  1746. dev_warn(dev, "Firmware failed to start\n");
  1747. ret = -EAGAIN;
  1748. goto exit;
  1749. }
  1750. /*
  1751. * Init H2C command
  1752. */
  1753. if (priv->rtl_chip == RTL8723B)
  1754. rtl8xxxu_write8(priv, REG_HMTFR, 0x0f);
  1755. exit:
  1756. return ret;
  1757. }
  1758. static int rtl8xxxu_download_firmware(struct rtl8xxxu_priv *priv)
  1759. {
  1760. int pages, remainder, i, ret;
  1761. u8 val8;
  1762. u16 val16;
  1763. u32 val32;
  1764. u8 *fwptr;
  1765. val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC + 1);
  1766. val8 |= 4;
  1767. rtl8xxxu_write8(priv, REG_SYS_FUNC + 1, val8);
  1768. /* 8051 enable */
  1769. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1770. val16 |= SYS_FUNC_CPU_ENABLE;
  1771. rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
  1772. val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
  1773. if (val8 & MCU_FW_RAM_SEL) {
  1774. pr_info("do the RAM reset\n");
  1775. rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
  1776. priv->fops->reset_8051(priv);
  1777. }
  1778. /* MCU firmware download enable */
  1779. val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
  1780. val8 |= MCU_FW_DL_ENABLE;
  1781. rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
  1782. /* 8051 reset */
  1783. val32 = rtl8xxxu_read32(priv, REG_MCU_FW_DL);
  1784. val32 &= ~BIT(19);
  1785. rtl8xxxu_write32(priv, REG_MCU_FW_DL, val32);
  1786. /* Reset firmware download checksum */
  1787. val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL);
  1788. val8 |= MCU_FW_DL_CSUM_REPORT;
  1789. rtl8xxxu_write8(priv, REG_MCU_FW_DL, val8);
  1790. pages = priv->fw_size / RTL_FW_PAGE_SIZE;
  1791. remainder = priv->fw_size % RTL_FW_PAGE_SIZE;
  1792. fwptr = priv->fw_data->data;
  1793. for (i = 0; i < pages; i++) {
  1794. val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
  1795. val8 |= i;
  1796. rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
  1797. ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
  1798. fwptr, RTL_FW_PAGE_SIZE);
  1799. if (ret != RTL_FW_PAGE_SIZE) {
  1800. ret = -EAGAIN;
  1801. goto fw_abort;
  1802. }
  1803. fwptr += RTL_FW_PAGE_SIZE;
  1804. }
  1805. if (remainder) {
  1806. val8 = rtl8xxxu_read8(priv, REG_MCU_FW_DL + 2) & 0xF8;
  1807. val8 |= i;
  1808. rtl8xxxu_write8(priv, REG_MCU_FW_DL + 2, val8);
  1809. ret = rtl8xxxu_writeN(priv, REG_FW_START_ADDRESS,
  1810. fwptr, remainder);
  1811. if (ret != remainder) {
  1812. ret = -EAGAIN;
  1813. goto fw_abort;
  1814. }
  1815. }
  1816. ret = 0;
  1817. fw_abort:
  1818. /* MCU firmware download disable */
  1819. val16 = rtl8xxxu_read16(priv, REG_MCU_FW_DL);
  1820. val16 &= ~MCU_FW_DL_ENABLE;
  1821. rtl8xxxu_write16(priv, REG_MCU_FW_DL, val16);
  1822. return ret;
  1823. }
  1824. int rtl8xxxu_load_firmware(struct rtl8xxxu_priv *priv, char *fw_name)
  1825. {
  1826. struct device *dev = &priv->udev->dev;
  1827. const struct firmware *fw;
  1828. int ret = 0;
  1829. u16 signature;
  1830. dev_info(dev, "%s: Loading firmware %s\n", DRIVER_NAME, fw_name);
  1831. if (request_firmware(&fw, fw_name, &priv->udev->dev)) {
  1832. dev_warn(dev, "request_firmware(%s) failed\n", fw_name);
  1833. ret = -EAGAIN;
  1834. goto exit;
  1835. }
  1836. if (!fw) {
  1837. dev_warn(dev, "Firmware data not available\n");
  1838. ret = -EINVAL;
  1839. goto exit;
  1840. }
  1841. priv->fw_data = kmemdup(fw->data, fw->size, GFP_KERNEL);
  1842. if (!priv->fw_data) {
  1843. ret = -ENOMEM;
  1844. goto exit;
  1845. }
  1846. priv->fw_size = fw->size - sizeof(struct rtl8xxxu_firmware_header);
  1847. signature = le16_to_cpu(priv->fw_data->signature);
  1848. switch (signature & 0xfff0) {
  1849. case 0x92e0:
  1850. case 0x92c0:
  1851. case 0x88c0:
  1852. case 0x5300:
  1853. case 0x2300:
  1854. break;
  1855. default:
  1856. ret = -EINVAL;
  1857. dev_warn(dev, "%s: Invalid firmware signature: 0x%04x\n",
  1858. __func__, signature);
  1859. }
  1860. dev_info(dev, "Firmware revision %i.%i (signature 0x%04x)\n",
  1861. le16_to_cpu(priv->fw_data->major_version),
  1862. priv->fw_data->minor_version, signature);
  1863. exit:
  1864. release_firmware(fw);
  1865. return ret;
  1866. }
  1867. void rtl8xxxu_firmware_self_reset(struct rtl8xxxu_priv *priv)
  1868. {
  1869. u16 val16;
  1870. int i = 100;
  1871. /* Inform 8051 to perform reset */
  1872. rtl8xxxu_write8(priv, REG_HMTFR + 3, 0x20);
  1873. for (i = 100; i > 0; i--) {
  1874. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1875. if (!(val16 & SYS_FUNC_CPU_ENABLE)) {
  1876. dev_dbg(&priv->udev->dev,
  1877. "%s: Firmware self reset success!\n", __func__);
  1878. break;
  1879. }
  1880. udelay(50);
  1881. }
  1882. if (!i) {
  1883. /* Force firmware reset */
  1884. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1885. val16 &= ~SYS_FUNC_CPU_ENABLE;
  1886. rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
  1887. }
  1888. }
  1889. static int
  1890. rtl8xxxu_init_mac(struct rtl8xxxu_priv *priv)
  1891. {
  1892. struct rtl8xxxu_reg8val *array = priv->fops->mactable;
  1893. int i, ret;
  1894. u16 reg;
  1895. u8 val;
  1896. for (i = 0; ; i++) {
  1897. reg = array[i].reg;
  1898. val = array[i].val;
  1899. if (reg == 0xffff && val == 0xff)
  1900. break;
  1901. ret = rtl8xxxu_write8(priv, reg, val);
  1902. if (ret != 1) {
  1903. dev_warn(&priv->udev->dev,
  1904. "Failed to initialize MAC "
  1905. "(reg: %04x, val %02x)\n", reg, val);
  1906. return -EAGAIN;
  1907. }
  1908. }
  1909. if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
  1910. rtl8xxxu_write8(priv, REG_MAX_AGGR_NUM, 0x0a);
  1911. return 0;
  1912. }
  1913. int rtl8xxxu_init_phy_regs(struct rtl8xxxu_priv *priv,
  1914. struct rtl8xxxu_reg32val *array)
  1915. {
  1916. int i, ret;
  1917. u16 reg;
  1918. u32 val;
  1919. for (i = 0; ; i++) {
  1920. reg = array[i].reg;
  1921. val = array[i].val;
  1922. if (reg == 0xffff && val == 0xffffffff)
  1923. break;
  1924. ret = rtl8xxxu_write32(priv, reg, val);
  1925. if (ret != sizeof(val)) {
  1926. dev_warn(&priv->udev->dev,
  1927. "Failed to initialize PHY\n");
  1928. return -EAGAIN;
  1929. }
  1930. udelay(1);
  1931. }
  1932. return 0;
  1933. }
  1934. void rtl8xxxu_gen1_init_phy_bb(struct rtl8xxxu_priv *priv)
  1935. {
  1936. u8 val8, ldoa15, ldov12d, lpldo, ldohci12;
  1937. u16 val16;
  1938. u32 val32;
  1939. val8 = rtl8xxxu_read8(priv, REG_AFE_PLL_CTRL);
  1940. udelay(2);
  1941. val8 |= AFE_PLL_320_ENABLE;
  1942. rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL, val8);
  1943. udelay(2);
  1944. rtl8xxxu_write8(priv, REG_AFE_PLL_CTRL + 1, 0xff);
  1945. udelay(2);
  1946. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  1947. val16 |= SYS_FUNC_BB_GLB_RSTN | SYS_FUNC_BBRSTB;
  1948. rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
  1949. val32 = rtl8xxxu_read32(priv, REG_AFE_XTAL_CTRL);
  1950. val32 &= ~AFE_XTAL_RF_GATE;
  1951. if (priv->has_bluetooth)
  1952. val32 &= ~AFE_XTAL_BT_GATE;
  1953. rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, val32);
  1954. /* 6. 0x1f[7:0] = 0x07 */
  1955. val8 = RF_ENABLE | RF_RSTB | RF_SDMRSTB;
  1956. rtl8xxxu_write8(priv, REG_RF_CTRL, val8);
  1957. if (priv->hi_pa)
  1958. rtl8xxxu_init_phy_regs(priv, rtl8188ru_phy_1t_highpa_table);
  1959. else if (priv->tx_paths == 2)
  1960. rtl8xxxu_init_phy_regs(priv, rtl8192cu_phy_2t_init_table);
  1961. else
  1962. rtl8xxxu_init_phy_regs(priv, rtl8723a_phy_1t_init_table);
  1963. if (priv->rtl_chip == RTL8188R && priv->hi_pa &&
  1964. priv->vendor_umc && priv->chip_cut == 1)
  1965. rtl8xxxu_write8(priv, REG_OFDM0_AGC_PARM1 + 2, 0x50);
  1966. if (priv->hi_pa)
  1967. rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_highpa_table);
  1968. else
  1969. rtl8xxxu_init_phy_regs(priv, rtl8xxx_agc_standard_table);
  1970. ldoa15 = LDOA15_ENABLE | LDOA15_OBUF;
  1971. ldov12d = LDOV12D_ENABLE | BIT(2) | (2 << LDOV12D_VADJ_SHIFT);
  1972. ldohci12 = 0x57;
  1973. lpldo = 1;
  1974. val32 = (lpldo << 24) | (ldohci12 << 16) | (ldov12d << 8) | ldoa15;
  1975. rtl8xxxu_write32(priv, REG_LDOA15_CTRL, val32);
  1976. }
  1977. /*
  1978. * Most of this is black magic retrieved from the old rtl8723au driver
  1979. */
  1980. static int rtl8xxxu_init_phy_bb(struct rtl8xxxu_priv *priv)
  1981. {
  1982. u8 val8;
  1983. u32 val32;
  1984. priv->fops->init_phy_bb(priv);
  1985. if (priv->tx_paths == 1 && priv->rx_paths == 2) {
  1986. /*
  1987. * For 1T2R boards, patch the registers.
  1988. *
  1989. * It looks like 8191/2 1T2R boards use path B for TX
  1990. */
  1991. val32 = rtl8xxxu_read32(priv, REG_FPGA0_TX_INFO);
  1992. val32 &= ~(BIT(0) | BIT(1));
  1993. val32 |= BIT(1);
  1994. rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, val32);
  1995. val32 = rtl8xxxu_read32(priv, REG_FPGA1_TX_INFO);
  1996. val32 &= ~0x300033;
  1997. val32 |= 0x200022;
  1998. rtl8xxxu_write32(priv, REG_FPGA1_TX_INFO, val32);
  1999. val32 = rtl8xxxu_read32(priv, REG_CCK0_AFE_SETTING);
  2000. val32 &= ~CCK0_AFE_RX_MASK;
  2001. val32 &= 0x00ffffff;
  2002. val32 |= 0x40000000;
  2003. val32 |= CCK0_AFE_RX_ANT_B;
  2004. rtl8xxxu_write32(priv, REG_CCK0_AFE_SETTING, val32);
  2005. val32 = rtl8xxxu_read32(priv, REG_OFDM0_TRX_PATH_ENABLE);
  2006. val32 &= ~(OFDM_RF_PATH_RX_MASK | OFDM_RF_PATH_TX_MASK);
  2007. val32 |= (OFDM_RF_PATH_RX_A | OFDM_RF_PATH_RX_B |
  2008. OFDM_RF_PATH_TX_B);
  2009. rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, val32);
  2010. val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGC_PARM1);
  2011. val32 &= ~(BIT(4) | BIT(5));
  2012. val32 |= BIT(4);
  2013. rtl8xxxu_write32(priv, REG_OFDM0_AGC_PARM1, val32);
  2014. val32 = rtl8xxxu_read32(priv, REG_TX_CCK_RFON);
  2015. val32 &= ~(BIT(27) | BIT(26));
  2016. val32 |= BIT(27);
  2017. rtl8xxxu_write32(priv, REG_TX_CCK_RFON, val32);
  2018. val32 = rtl8xxxu_read32(priv, REG_TX_CCK_BBON);
  2019. val32 &= ~(BIT(27) | BIT(26));
  2020. val32 |= BIT(27);
  2021. rtl8xxxu_write32(priv, REG_TX_CCK_BBON, val32);
  2022. val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_RFON);
  2023. val32 &= ~(BIT(27) | BIT(26));
  2024. val32 |= BIT(27);
  2025. rtl8xxxu_write32(priv, REG_TX_OFDM_RFON, val32);
  2026. val32 = rtl8xxxu_read32(priv, REG_TX_OFDM_BBON);
  2027. val32 &= ~(BIT(27) | BIT(26));
  2028. val32 |= BIT(27);
  2029. rtl8xxxu_write32(priv, REG_TX_OFDM_BBON, val32);
  2030. val32 = rtl8xxxu_read32(priv, REG_TX_TO_TX);
  2031. val32 &= ~(BIT(27) | BIT(26));
  2032. val32 |= BIT(27);
  2033. rtl8xxxu_write32(priv, REG_TX_TO_TX, val32);
  2034. }
  2035. if (priv->has_xtalk) {
  2036. val32 = rtl8xxxu_read32(priv, REG_MAC_PHY_CTRL);
  2037. val8 = priv->xtalk;
  2038. val32 &= 0xff000fff;
  2039. val32 |= ((val8 | (val8 << 6)) << 12);
  2040. rtl8xxxu_write32(priv, REG_MAC_PHY_CTRL, val32);
  2041. }
  2042. if (priv->rtl_chip == RTL8192E)
  2043. rtl8xxxu_write32(priv, REG_AFE_XTAL_CTRL, 0x000f81fb);
  2044. return 0;
  2045. }
  2046. static int rtl8xxxu_init_rf_regs(struct rtl8xxxu_priv *priv,
  2047. struct rtl8xxxu_rfregval *array,
  2048. enum rtl8xxxu_rfpath path)
  2049. {
  2050. int i, ret;
  2051. u8 reg;
  2052. u32 val;
  2053. for (i = 0; ; i++) {
  2054. reg = array[i].reg;
  2055. val = array[i].val;
  2056. if (reg == 0xff && val == 0xffffffff)
  2057. break;
  2058. switch (reg) {
  2059. case 0xfe:
  2060. msleep(50);
  2061. continue;
  2062. case 0xfd:
  2063. mdelay(5);
  2064. continue;
  2065. case 0xfc:
  2066. mdelay(1);
  2067. continue;
  2068. case 0xfb:
  2069. udelay(50);
  2070. continue;
  2071. case 0xfa:
  2072. udelay(5);
  2073. continue;
  2074. case 0xf9:
  2075. udelay(1);
  2076. continue;
  2077. }
  2078. ret = rtl8xxxu_write_rfreg(priv, path, reg, val);
  2079. if (ret) {
  2080. dev_warn(&priv->udev->dev,
  2081. "Failed to initialize RF\n");
  2082. return -EAGAIN;
  2083. }
  2084. udelay(1);
  2085. }
  2086. return 0;
  2087. }
  2088. int rtl8xxxu_init_phy_rf(struct rtl8xxxu_priv *priv,
  2089. struct rtl8xxxu_rfregval *table,
  2090. enum rtl8xxxu_rfpath path)
  2091. {
  2092. u32 val32;
  2093. u16 val16, rfsi_rfenv;
  2094. u16 reg_sw_ctrl, reg_int_oe, reg_hssi_parm2;
  2095. switch (path) {
  2096. case RF_A:
  2097. reg_sw_ctrl = REG_FPGA0_XA_RF_SW_CTRL;
  2098. reg_int_oe = REG_FPGA0_XA_RF_INT_OE;
  2099. reg_hssi_parm2 = REG_FPGA0_XA_HSSI_PARM2;
  2100. break;
  2101. case RF_B:
  2102. reg_sw_ctrl = REG_FPGA0_XB_RF_SW_CTRL;
  2103. reg_int_oe = REG_FPGA0_XB_RF_INT_OE;
  2104. reg_hssi_parm2 = REG_FPGA0_XB_HSSI_PARM2;
  2105. break;
  2106. default:
  2107. dev_err(&priv->udev->dev, "%s:Unsupported RF path %c\n",
  2108. __func__, path + 'A');
  2109. return -EINVAL;
  2110. }
  2111. /* For path B, use XB */
  2112. rfsi_rfenv = rtl8xxxu_read16(priv, reg_sw_ctrl);
  2113. rfsi_rfenv &= FPGA0_RF_RFENV;
  2114. /*
  2115. * These two we might be able to optimize into one
  2116. */
  2117. val32 = rtl8xxxu_read32(priv, reg_int_oe);
  2118. val32 |= BIT(20); /* 0x10 << 16 */
  2119. rtl8xxxu_write32(priv, reg_int_oe, val32);
  2120. udelay(1);
  2121. val32 = rtl8xxxu_read32(priv, reg_int_oe);
  2122. val32 |= BIT(4);
  2123. rtl8xxxu_write32(priv, reg_int_oe, val32);
  2124. udelay(1);
  2125. /*
  2126. * These two we might be able to optimize into one
  2127. */
  2128. val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
  2129. val32 &= ~FPGA0_HSSI_3WIRE_ADDR_LEN;
  2130. rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
  2131. udelay(1);
  2132. val32 = rtl8xxxu_read32(priv, reg_hssi_parm2);
  2133. val32 &= ~FPGA0_HSSI_3WIRE_DATA_LEN;
  2134. rtl8xxxu_write32(priv, reg_hssi_parm2, val32);
  2135. udelay(1);
  2136. rtl8xxxu_init_rf_regs(priv, table, path);
  2137. /* For path B, use XB */
  2138. val16 = rtl8xxxu_read16(priv, reg_sw_ctrl);
  2139. val16 &= ~FPGA0_RF_RFENV;
  2140. val16 |= rfsi_rfenv;
  2141. rtl8xxxu_write16(priv, reg_sw_ctrl, val16);
  2142. return 0;
  2143. }
  2144. static int rtl8xxxu_llt_write(struct rtl8xxxu_priv *priv, u8 address, u8 data)
  2145. {
  2146. int ret = -EBUSY;
  2147. int count = 0;
  2148. u32 value;
  2149. value = LLT_OP_WRITE | address << 8 | data;
  2150. rtl8xxxu_write32(priv, REG_LLT_INIT, value);
  2151. do {
  2152. value = rtl8xxxu_read32(priv, REG_LLT_INIT);
  2153. if ((value & LLT_OP_MASK) == LLT_OP_INACTIVE) {
  2154. ret = 0;
  2155. break;
  2156. }
  2157. } while (count++ < 20);
  2158. return ret;
  2159. }
  2160. int rtl8xxxu_init_llt_table(struct rtl8xxxu_priv *priv)
  2161. {
  2162. int ret;
  2163. int i;
  2164. u8 last_tx_page;
  2165. last_tx_page = priv->fops->total_page_num;
  2166. for (i = 0; i < last_tx_page; i++) {
  2167. ret = rtl8xxxu_llt_write(priv, i, i + 1);
  2168. if (ret)
  2169. goto exit;
  2170. }
  2171. ret = rtl8xxxu_llt_write(priv, last_tx_page, 0xff);
  2172. if (ret)
  2173. goto exit;
  2174. /* Mark remaining pages as a ring buffer */
  2175. for (i = last_tx_page + 1; i < 0xff; i++) {
  2176. ret = rtl8xxxu_llt_write(priv, i, (i + 1));
  2177. if (ret)
  2178. goto exit;
  2179. }
  2180. /* Let last entry point to the start entry of ring buffer */
  2181. ret = rtl8xxxu_llt_write(priv, 0xff, last_tx_page + 1);
  2182. if (ret)
  2183. goto exit;
  2184. exit:
  2185. return ret;
  2186. }
  2187. int rtl8xxxu_auto_llt_table(struct rtl8xxxu_priv *priv)
  2188. {
  2189. u32 val32;
  2190. int ret = 0;
  2191. int i;
  2192. val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
  2193. val32 |= AUTO_LLT_INIT_LLT;
  2194. rtl8xxxu_write32(priv, REG_AUTO_LLT, val32);
  2195. for (i = 500; i; i--) {
  2196. val32 = rtl8xxxu_read32(priv, REG_AUTO_LLT);
  2197. if (!(val32 & AUTO_LLT_INIT_LLT))
  2198. break;
  2199. usleep_range(2, 4);
  2200. }
  2201. if (!i) {
  2202. ret = -EBUSY;
  2203. dev_warn(&priv->udev->dev, "LLT table init failed\n");
  2204. }
  2205. return ret;
  2206. }
  2207. static int rtl8xxxu_init_queue_priority(struct rtl8xxxu_priv *priv)
  2208. {
  2209. u16 val16, hi, lo;
  2210. u16 hiq, mgq, bkq, beq, viq, voq;
  2211. int hip, mgp, bkp, bep, vip, vop;
  2212. int ret = 0;
  2213. switch (priv->ep_tx_count) {
  2214. case 1:
  2215. if (priv->ep_tx_high_queue) {
  2216. hi = TRXDMA_QUEUE_HIGH;
  2217. } else if (priv->ep_tx_low_queue) {
  2218. hi = TRXDMA_QUEUE_LOW;
  2219. } else if (priv->ep_tx_normal_queue) {
  2220. hi = TRXDMA_QUEUE_NORMAL;
  2221. } else {
  2222. hi = 0;
  2223. ret = -EINVAL;
  2224. }
  2225. hiq = hi;
  2226. mgq = hi;
  2227. bkq = hi;
  2228. beq = hi;
  2229. viq = hi;
  2230. voq = hi;
  2231. hip = 0;
  2232. mgp = 0;
  2233. bkp = 0;
  2234. bep = 0;
  2235. vip = 0;
  2236. vop = 0;
  2237. break;
  2238. case 2:
  2239. if (priv->ep_tx_high_queue && priv->ep_tx_low_queue) {
  2240. hi = TRXDMA_QUEUE_HIGH;
  2241. lo = TRXDMA_QUEUE_LOW;
  2242. } else if (priv->ep_tx_normal_queue && priv->ep_tx_low_queue) {
  2243. hi = TRXDMA_QUEUE_NORMAL;
  2244. lo = TRXDMA_QUEUE_LOW;
  2245. } else if (priv->ep_tx_high_queue && priv->ep_tx_normal_queue) {
  2246. hi = TRXDMA_QUEUE_HIGH;
  2247. lo = TRXDMA_QUEUE_NORMAL;
  2248. } else {
  2249. ret = -EINVAL;
  2250. hi = 0;
  2251. lo = 0;
  2252. }
  2253. hiq = hi;
  2254. mgq = hi;
  2255. bkq = lo;
  2256. beq = lo;
  2257. viq = hi;
  2258. voq = hi;
  2259. hip = 0;
  2260. mgp = 0;
  2261. bkp = 1;
  2262. bep = 1;
  2263. vip = 0;
  2264. vop = 0;
  2265. break;
  2266. case 3:
  2267. beq = TRXDMA_QUEUE_LOW;
  2268. bkq = TRXDMA_QUEUE_LOW;
  2269. viq = TRXDMA_QUEUE_NORMAL;
  2270. voq = TRXDMA_QUEUE_HIGH;
  2271. mgq = TRXDMA_QUEUE_HIGH;
  2272. hiq = TRXDMA_QUEUE_HIGH;
  2273. hip = hiq ^ 3;
  2274. mgp = mgq ^ 3;
  2275. bkp = bkq ^ 3;
  2276. bep = beq ^ 3;
  2277. vip = viq ^ 3;
  2278. vop = viq ^ 3;
  2279. break;
  2280. default:
  2281. ret = -EINVAL;
  2282. }
  2283. /*
  2284. * None of the vendor drivers are configuring the beacon
  2285. * queue here .... why?
  2286. */
  2287. if (!ret) {
  2288. val16 = rtl8xxxu_read16(priv, REG_TRXDMA_CTRL);
  2289. val16 &= 0x7;
  2290. val16 |= (voq << TRXDMA_CTRL_VOQ_SHIFT) |
  2291. (viq << TRXDMA_CTRL_VIQ_SHIFT) |
  2292. (beq << TRXDMA_CTRL_BEQ_SHIFT) |
  2293. (bkq << TRXDMA_CTRL_BKQ_SHIFT) |
  2294. (mgq << TRXDMA_CTRL_MGQ_SHIFT) |
  2295. (hiq << TRXDMA_CTRL_HIQ_SHIFT);
  2296. rtl8xxxu_write16(priv, REG_TRXDMA_CTRL, val16);
  2297. priv->pipe_out[TXDESC_QUEUE_VO] =
  2298. usb_sndbulkpipe(priv->udev, priv->out_ep[vop]);
  2299. priv->pipe_out[TXDESC_QUEUE_VI] =
  2300. usb_sndbulkpipe(priv->udev, priv->out_ep[vip]);
  2301. priv->pipe_out[TXDESC_QUEUE_BE] =
  2302. usb_sndbulkpipe(priv->udev, priv->out_ep[bep]);
  2303. priv->pipe_out[TXDESC_QUEUE_BK] =
  2304. usb_sndbulkpipe(priv->udev, priv->out_ep[bkp]);
  2305. priv->pipe_out[TXDESC_QUEUE_BEACON] =
  2306. usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
  2307. priv->pipe_out[TXDESC_QUEUE_MGNT] =
  2308. usb_sndbulkpipe(priv->udev, priv->out_ep[mgp]);
  2309. priv->pipe_out[TXDESC_QUEUE_HIGH] =
  2310. usb_sndbulkpipe(priv->udev, priv->out_ep[hip]);
  2311. priv->pipe_out[TXDESC_QUEUE_CMD] =
  2312. usb_sndbulkpipe(priv->udev, priv->out_ep[0]);
  2313. }
  2314. return ret;
  2315. }
  2316. void rtl8xxxu_fill_iqk_matrix_a(struct rtl8xxxu_priv *priv, bool iqk_ok,
  2317. int result[][8], int candidate, bool tx_only)
  2318. {
  2319. u32 oldval, x, tx0_a, reg;
  2320. int y, tx0_c;
  2321. u32 val32;
  2322. if (!iqk_ok)
  2323. return;
  2324. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
  2325. oldval = val32 >> 22;
  2326. x = result[candidate][0];
  2327. if ((x & 0x00000200) != 0)
  2328. x = x | 0xfffffc00;
  2329. tx0_a = (x * oldval) >> 8;
  2330. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
  2331. val32 &= ~0x3ff;
  2332. val32 |= tx0_a;
  2333. rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
  2334. val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
  2335. val32 &= ~BIT(31);
  2336. if ((x * oldval >> 7) & 0x1)
  2337. val32 |= BIT(31);
  2338. rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
  2339. y = result[candidate][1];
  2340. if ((y & 0x00000200) != 0)
  2341. y = y | 0xfffffc00;
  2342. tx0_c = (y * oldval) >> 8;
  2343. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XC_TX_AFE);
  2344. val32 &= ~0xf0000000;
  2345. val32 |= (((tx0_c & 0x3c0) >> 6) << 28);
  2346. rtl8xxxu_write32(priv, REG_OFDM0_XC_TX_AFE, val32);
  2347. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE);
  2348. val32 &= ~0x003f0000;
  2349. val32 |= ((tx0_c & 0x3f) << 16);
  2350. rtl8xxxu_write32(priv, REG_OFDM0_XA_TX_IQ_IMBALANCE, val32);
  2351. val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
  2352. val32 &= ~BIT(29);
  2353. if ((y * oldval >> 7) & 0x1)
  2354. val32 |= BIT(29);
  2355. rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
  2356. if (tx_only) {
  2357. dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
  2358. return;
  2359. }
  2360. reg = result[candidate][2];
  2361. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
  2362. val32 &= ~0x3ff;
  2363. val32 |= (reg & 0x3ff);
  2364. rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
  2365. reg = result[candidate][3] & 0x3F;
  2366. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE);
  2367. val32 &= ~0xfc00;
  2368. val32 |= ((reg << 10) & 0xfc00);
  2369. rtl8xxxu_write32(priv, REG_OFDM0_XA_RX_IQ_IMBALANCE, val32);
  2370. reg = (result[candidate][3] >> 6) & 0xF;
  2371. val32 = rtl8xxxu_read32(priv, REG_OFDM0_RX_IQ_EXT_ANTA);
  2372. val32 &= ~0xf0000000;
  2373. val32 |= (reg << 28);
  2374. rtl8xxxu_write32(priv, REG_OFDM0_RX_IQ_EXT_ANTA, val32);
  2375. }
  2376. void rtl8xxxu_fill_iqk_matrix_b(struct rtl8xxxu_priv *priv, bool iqk_ok,
  2377. int result[][8], int candidate, bool tx_only)
  2378. {
  2379. u32 oldval, x, tx1_a, reg;
  2380. int y, tx1_c;
  2381. u32 val32;
  2382. if (!iqk_ok)
  2383. return;
  2384. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
  2385. oldval = val32 >> 22;
  2386. x = result[candidate][4];
  2387. if ((x & 0x00000200) != 0)
  2388. x = x | 0xfffffc00;
  2389. tx1_a = (x * oldval) >> 8;
  2390. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
  2391. val32 &= ~0x3ff;
  2392. val32 |= tx1_a;
  2393. rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
  2394. val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
  2395. val32 &= ~BIT(27);
  2396. if ((x * oldval >> 7) & 0x1)
  2397. val32 |= BIT(27);
  2398. rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
  2399. y = result[candidate][5];
  2400. if ((y & 0x00000200) != 0)
  2401. y = y | 0xfffffc00;
  2402. tx1_c = (y * oldval) >> 8;
  2403. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XD_TX_AFE);
  2404. val32 &= ~0xf0000000;
  2405. val32 |= (((tx1_c & 0x3c0) >> 6) << 28);
  2406. rtl8xxxu_write32(priv, REG_OFDM0_XD_TX_AFE, val32);
  2407. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE);
  2408. val32 &= ~0x003f0000;
  2409. val32 |= ((tx1_c & 0x3f) << 16);
  2410. rtl8xxxu_write32(priv, REG_OFDM0_XB_TX_IQ_IMBALANCE, val32);
  2411. val32 = rtl8xxxu_read32(priv, REG_OFDM0_ENERGY_CCA_THRES);
  2412. val32 &= ~BIT(25);
  2413. if ((y * oldval >> 7) & 0x1)
  2414. val32 |= BIT(25);
  2415. rtl8xxxu_write32(priv, REG_OFDM0_ENERGY_CCA_THRES, val32);
  2416. if (tx_only) {
  2417. dev_dbg(&priv->udev->dev, "%s: only TX\n", __func__);
  2418. return;
  2419. }
  2420. reg = result[candidate][6];
  2421. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
  2422. val32 &= ~0x3ff;
  2423. val32 |= (reg & 0x3ff);
  2424. rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
  2425. reg = result[candidate][7] & 0x3f;
  2426. val32 = rtl8xxxu_read32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE);
  2427. val32 &= ~0xfc00;
  2428. val32 |= ((reg << 10) & 0xfc00);
  2429. rtl8xxxu_write32(priv, REG_OFDM0_XB_RX_IQ_IMBALANCE, val32);
  2430. reg = (result[candidate][7] >> 6) & 0xf;
  2431. val32 = rtl8xxxu_read32(priv, REG_OFDM0_AGCR_SSI_TABLE);
  2432. val32 &= ~0x0000f000;
  2433. val32 |= (reg << 12);
  2434. rtl8xxxu_write32(priv, REG_OFDM0_AGCR_SSI_TABLE, val32);
  2435. }
  2436. #define MAX_TOLERANCE 5
  2437. static bool rtl8xxxu_simularity_compare(struct rtl8xxxu_priv *priv,
  2438. int result[][8], int c1, int c2)
  2439. {
  2440. u32 i, j, diff, simubitmap, bound = 0;
  2441. int candidate[2] = {-1, -1}; /* for path A and path B */
  2442. bool retval = true;
  2443. if (priv->tx_paths > 1)
  2444. bound = 8;
  2445. else
  2446. bound = 4;
  2447. simubitmap = 0;
  2448. for (i = 0; i < bound; i++) {
  2449. diff = (result[c1][i] > result[c2][i]) ?
  2450. (result[c1][i] - result[c2][i]) :
  2451. (result[c2][i] - result[c1][i]);
  2452. if (diff > MAX_TOLERANCE) {
  2453. if ((i == 2 || i == 6) && !simubitmap) {
  2454. if (result[c1][i] + result[c1][i + 1] == 0)
  2455. candidate[(i / 4)] = c2;
  2456. else if (result[c2][i] + result[c2][i + 1] == 0)
  2457. candidate[(i / 4)] = c1;
  2458. else
  2459. simubitmap = simubitmap | (1 << i);
  2460. } else {
  2461. simubitmap = simubitmap | (1 << i);
  2462. }
  2463. }
  2464. }
  2465. if (simubitmap == 0) {
  2466. for (i = 0; i < (bound / 4); i++) {
  2467. if (candidate[i] >= 0) {
  2468. for (j = i * 4; j < (i + 1) * 4 - 2; j++)
  2469. result[3][j] = result[candidate[i]][j];
  2470. retval = false;
  2471. }
  2472. }
  2473. return retval;
  2474. } else if (!(simubitmap & 0x0f)) {
  2475. /* path A OK */
  2476. for (i = 0; i < 4; i++)
  2477. result[3][i] = result[c1][i];
  2478. } else if (!(simubitmap & 0xf0) && priv->tx_paths > 1) {
  2479. /* path B OK */
  2480. for (i = 4; i < 8; i++)
  2481. result[3][i] = result[c1][i];
  2482. }
  2483. return false;
  2484. }
  2485. bool rtl8xxxu_gen2_simularity_compare(struct rtl8xxxu_priv *priv,
  2486. int result[][8], int c1, int c2)
  2487. {
  2488. u32 i, j, diff, simubitmap, bound = 0;
  2489. int candidate[2] = {-1, -1}; /* for path A and path B */
  2490. int tmp1, tmp2;
  2491. bool retval = true;
  2492. if (priv->tx_paths > 1)
  2493. bound = 8;
  2494. else
  2495. bound = 4;
  2496. simubitmap = 0;
  2497. for (i = 0; i < bound; i++) {
  2498. if (i & 1) {
  2499. if ((result[c1][i] & 0x00000200))
  2500. tmp1 = result[c1][i] | 0xfffffc00;
  2501. else
  2502. tmp1 = result[c1][i];
  2503. if ((result[c2][i]& 0x00000200))
  2504. tmp2 = result[c2][i] | 0xfffffc00;
  2505. else
  2506. tmp2 = result[c2][i];
  2507. } else {
  2508. tmp1 = result[c1][i];
  2509. tmp2 = result[c2][i];
  2510. }
  2511. diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
  2512. if (diff > MAX_TOLERANCE) {
  2513. if ((i == 2 || i == 6) && !simubitmap) {
  2514. if (result[c1][i] + result[c1][i + 1] == 0)
  2515. candidate[(i / 4)] = c2;
  2516. else if (result[c2][i] + result[c2][i + 1] == 0)
  2517. candidate[(i / 4)] = c1;
  2518. else
  2519. simubitmap = simubitmap | (1 << i);
  2520. } else {
  2521. simubitmap = simubitmap | (1 << i);
  2522. }
  2523. }
  2524. }
  2525. if (simubitmap == 0) {
  2526. for (i = 0; i < (bound / 4); i++) {
  2527. if (candidate[i] >= 0) {
  2528. for (j = i * 4; j < (i + 1) * 4 - 2; j++)
  2529. result[3][j] = result[candidate[i]][j];
  2530. retval = false;
  2531. }
  2532. }
  2533. return retval;
  2534. } else {
  2535. if (!(simubitmap & 0x03)) {
  2536. /* path A TX OK */
  2537. for (i = 0; i < 2; i++)
  2538. result[3][i] = result[c1][i];
  2539. }
  2540. if (!(simubitmap & 0x0c)) {
  2541. /* path A RX OK */
  2542. for (i = 2; i < 4; i++)
  2543. result[3][i] = result[c1][i];
  2544. }
  2545. if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
  2546. /* path B RX OK */
  2547. for (i = 4; i < 6; i++)
  2548. result[3][i] = result[c1][i];
  2549. }
  2550. if (!(simubitmap & 0x30) && priv->tx_paths > 1) {
  2551. /* path B RX OK */
  2552. for (i = 6; i < 8; i++)
  2553. result[3][i] = result[c1][i];
  2554. }
  2555. }
  2556. return false;
  2557. }
  2558. void
  2559. rtl8xxxu_save_mac_regs(struct rtl8xxxu_priv *priv, const u32 *reg, u32 *backup)
  2560. {
  2561. int i;
  2562. for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
  2563. backup[i] = rtl8xxxu_read8(priv, reg[i]);
  2564. backup[i] = rtl8xxxu_read32(priv, reg[i]);
  2565. }
  2566. void rtl8xxxu_restore_mac_regs(struct rtl8xxxu_priv *priv,
  2567. const u32 *reg, u32 *backup)
  2568. {
  2569. int i;
  2570. for (i = 0; i < (RTL8XXXU_MAC_REGS - 1); i++)
  2571. rtl8xxxu_write8(priv, reg[i], backup[i]);
  2572. rtl8xxxu_write32(priv, reg[i], backup[i]);
  2573. }
  2574. void rtl8xxxu_save_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
  2575. u32 *backup, int count)
  2576. {
  2577. int i;
  2578. for (i = 0; i < count; i++)
  2579. backup[i] = rtl8xxxu_read32(priv, regs[i]);
  2580. }
  2581. void rtl8xxxu_restore_regs(struct rtl8xxxu_priv *priv, const u32 *regs,
  2582. u32 *backup, int count)
  2583. {
  2584. int i;
  2585. for (i = 0; i < count; i++)
  2586. rtl8xxxu_write32(priv, regs[i], backup[i]);
  2587. }
  2588. void rtl8xxxu_path_adda_on(struct rtl8xxxu_priv *priv, const u32 *regs,
  2589. bool path_a_on)
  2590. {
  2591. u32 path_on;
  2592. int i;
  2593. if (priv->tx_paths == 1) {
  2594. path_on = priv->fops->adda_1t_path_on;
  2595. rtl8xxxu_write32(priv, regs[0], priv->fops->adda_1t_init);
  2596. } else {
  2597. path_on = path_a_on ? priv->fops->adda_2t_path_on_a :
  2598. priv->fops->adda_2t_path_on_b;
  2599. rtl8xxxu_write32(priv, regs[0], path_on);
  2600. }
  2601. for (i = 1 ; i < RTL8XXXU_ADDA_REGS ; i++)
  2602. rtl8xxxu_write32(priv, regs[i], path_on);
  2603. }
  2604. void rtl8xxxu_mac_calibration(struct rtl8xxxu_priv *priv,
  2605. const u32 *regs, u32 *backup)
  2606. {
  2607. int i = 0;
  2608. rtl8xxxu_write8(priv, regs[i], 0x3f);
  2609. for (i = 1 ; i < (RTL8XXXU_MAC_REGS - 1); i++)
  2610. rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(3)));
  2611. rtl8xxxu_write8(priv, regs[i], (u8)(backup[i] & ~BIT(5)));
  2612. }
  2613. static int rtl8xxxu_iqk_path_a(struct rtl8xxxu_priv *priv)
  2614. {
  2615. u32 reg_eac, reg_e94, reg_e9c, reg_ea4, val32;
  2616. int result = 0;
  2617. /* path-A IQK setting */
  2618. rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x10008c1f);
  2619. rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x10008c1f);
  2620. rtl8xxxu_write32(priv, REG_TX_IQK_PI_A, 0x82140102);
  2621. val32 = (priv->rf_paths > 1) ? 0x28160202 :
  2622. /*IS_81xxC_VENDOR_UMC_B_CUT(pHalData->VersionID)?0x28160202: */
  2623. 0x28160502;
  2624. rtl8xxxu_write32(priv, REG_RX_IQK_PI_A, val32);
  2625. /* path-B IQK setting */
  2626. if (priv->rf_paths > 1) {
  2627. rtl8xxxu_write32(priv, REG_TX_IQK_TONE_B, 0x10008c22);
  2628. rtl8xxxu_write32(priv, REG_RX_IQK_TONE_B, 0x10008c22);
  2629. rtl8xxxu_write32(priv, REG_TX_IQK_PI_B, 0x82140102);
  2630. rtl8xxxu_write32(priv, REG_RX_IQK_PI_B, 0x28160202);
  2631. }
  2632. /* LO calibration setting */
  2633. rtl8xxxu_write32(priv, REG_IQK_AGC_RSP, 0x001028d1);
  2634. /* One shot, path A LOK & IQK */
  2635. rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf9000000);
  2636. rtl8xxxu_write32(priv, REG_IQK_AGC_PTS, 0xf8000000);
  2637. mdelay(1);
  2638. /* Check failed */
  2639. reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
  2640. reg_e94 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_A);
  2641. reg_e9c = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_A);
  2642. reg_ea4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_A_2);
  2643. if (!(reg_eac & BIT(28)) &&
  2644. ((reg_e94 & 0x03ff0000) != 0x01420000) &&
  2645. ((reg_e9c & 0x03ff0000) != 0x00420000))
  2646. result |= 0x01;
  2647. else /* If TX not OK, ignore RX */
  2648. goto out;
  2649. /* If TX is OK, check whether RX is OK */
  2650. if (!(reg_eac & BIT(27)) &&
  2651. ((reg_ea4 & 0x03ff0000) != 0x01320000) &&
  2652. ((reg_eac & 0x03ff0000) != 0x00360000))
  2653. result |= 0x02;
  2654. else
  2655. dev_warn(&priv->udev->dev, "%s: Path A RX IQK failed!\n",
  2656. __func__);
  2657. out:
  2658. return result;
  2659. }
  2660. static int rtl8xxxu_iqk_path_b(struct rtl8xxxu_priv *priv)
  2661. {
  2662. u32 reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
  2663. int result = 0;
  2664. /* One shot, path B LOK & IQK */
  2665. rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000002);
  2666. rtl8xxxu_write32(priv, REG_IQK_AGC_CONT, 0x00000000);
  2667. mdelay(1);
  2668. /* Check failed */
  2669. reg_eac = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_A_2);
  2670. reg_eb4 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
  2671. reg_ebc = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
  2672. reg_ec4 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
  2673. reg_ecc = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
  2674. if (!(reg_eac & BIT(31)) &&
  2675. ((reg_eb4 & 0x03ff0000) != 0x01420000) &&
  2676. ((reg_ebc & 0x03ff0000) != 0x00420000))
  2677. result |= 0x01;
  2678. else
  2679. goto out;
  2680. if (!(reg_eac & BIT(30)) &&
  2681. (((reg_ec4 & 0x03ff0000) >> 16) != 0x132) &&
  2682. (((reg_ecc & 0x03ff0000) >> 16) != 0x36))
  2683. result |= 0x02;
  2684. else
  2685. dev_warn(&priv->udev->dev, "%s: Path B RX IQK failed!\n",
  2686. __func__);
  2687. out:
  2688. return result;
  2689. }
  2690. static void rtl8xxxu_phy_iqcalibrate(struct rtl8xxxu_priv *priv,
  2691. int result[][8], int t)
  2692. {
  2693. struct device *dev = &priv->udev->dev;
  2694. u32 i, val32;
  2695. int path_a_ok, path_b_ok;
  2696. int retry = 2;
  2697. const u32 adda_regs[RTL8XXXU_ADDA_REGS] = {
  2698. REG_FPGA0_XCD_SWITCH_CTRL, REG_BLUETOOTH,
  2699. REG_RX_WAIT_CCA, REG_TX_CCK_RFON,
  2700. REG_TX_CCK_BBON, REG_TX_OFDM_RFON,
  2701. REG_TX_OFDM_BBON, REG_TX_TO_RX,
  2702. REG_TX_TO_TX, REG_RX_CCK,
  2703. REG_RX_OFDM, REG_RX_WAIT_RIFS,
  2704. REG_RX_TO_RX, REG_STANDBY,
  2705. REG_SLEEP, REG_PMPD_ANAEN
  2706. };
  2707. const u32 iqk_mac_regs[RTL8XXXU_MAC_REGS] = {
  2708. REG_TXPAUSE, REG_BEACON_CTRL,
  2709. REG_BEACON_CTRL_1, REG_GPIO_MUXCFG
  2710. };
  2711. const u32 iqk_bb_regs[RTL8XXXU_BB_REGS] = {
  2712. REG_OFDM0_TRX_PATH_ENABLE, REG_OFDM0_TR_MUX_PAR,
  2713. REG_FPGA0_XCD_RF_SW_CTRL, REG_CONFIG_ANT_A, REG_CONFIG_ANT_B,
  2714. REG_FPGA0_XAB_RF_SW_CTRL, REG_FPGA0_XA_RF_INT_OE,
  2715. REG_FPGA0_XB_RF_INT_OE, REG_FPGA0_RF_MODE
  2716. };
  2717. /*
  2718. * Note: IQ calibration must be performed after loading
  2719. * PHY_REG.txt , and radio_a, radio_b.txt
  2720. */
  2721. if (t == 0) {
  2722. /* Save ADDA parameters, turn Path A ADDA on */
  2723. rtl8xxxu_save_regs(priv, adda_regs, priv->adda_backup,
  2724. RTL8XXXU_ADDA_REGS);
  2725. rtl8xxxu_save_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
  2726. rtl8xxxu_save_regs(priv, iqk_bb_regs,
  2727. priv->bb_backup, RTL8XXXU_BB_REGS);
  2728. }
  2729. rtl8xxxu_path_adda_on(priv, adda_regs, true);
  2730. if (t == 0) {
  2731. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_HSSI_PARM1);
  2732. if (val32 & FPGA0_HSSI_PARM1_PI)
  2733. priv->pi_enabled = 1;
  2734. }
  2735. if (!priv->pi_enabled) {
  2736. /* Switch BB to PI mode to do IQ Calibration. */
  2737. rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, 0x01000100);
  2738. rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, 0x01000100);
  2739. }
  2740. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  2741. val32 &= ~FPGA_RF_MODE_CCK;
  2742. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  2743. rtl8xxxu_write32(priv, REG_OFDM0_TRX_PATH_ENABLE, 0x03a05600);
  2744. rtl8xxxu_write32(priv, REG_OFDM0_TR_MUX_PAR, 0x000800e4);
  2745. rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_SW_CTRL, 0x22204000);
  2746. if (!priv->no_pape) {
  2747. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XAB_RF_SW_CTRL);
  2748. val32 |= (FPGA0_RF_PAPE |
  2749. (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
  2750. rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
  2751. }
  2752. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XA_RF_INT_OE);
  2753. val32 &= ~BIT(10);
  2754. rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, val32);
  2755. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XB_RF_INT_OE);
  2756. val32 &= ~BIT(10);
  2757. rtl8xxxu_write32(priv, REG_FPGA0_XB_RF_INT_OE, val32);
  2758. if (priv->tx_paths > 1) {
  2759. rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
  2760. rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM, 0x00010000);
  2761. }
  2762. /* MAC settings */
  2763. rtl8xxxu_mac_calibration(priv, iqk_mac_regs, priv->mac_backup);
  2764. /* Page B init */
  2765. rtl8xxxu_write32(priv, REG_CONFIG_ANT_A, 0x00080000);
  2766. if (priv->tx_paths > 1)
  2767. rtl8xxxu_write32(priv, REG_CONFIG_ANT_B, 0x00080000);
  2768. /* IQ calibration setting */
  2769. rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
  2770. rtl8xxxu_write32(priv, REG_TX_IQK, 0x01007c00);
  2771. rtl8xxxu_write32(priv, REG_RX_IQK, 0x01004800);
  2772. for (i = 0; i < retry; i++) {
  2773. path_a_ok = rtl8xxxu_iqk_path_a(priv);
  2774. if (path_a_ok == 0x03) {
  2775. val32 = rtl8xxxu_read32(priv,
  2776. REG_TX_POWER_BEFORE_IQK_A);
  2777. result[t][0] = (val32 >> 16) & 0x3ff;
  2778. val32 = rtl8xxxu_read32(priv,
  2779. REG_TX_POWER_AFTER_IQK_A);
  2780. result[t][1] = (val32 >> 16) & 0x3ff;
  2781. val32 = rtl8xxxu_read32(priv,
  2782. REG_RX_POWER_BEFORE_IQK_A_2);
  2783. result[t][2] = (val32 >> 16) & 0x3ff;
  2784. val32 = rtl8xxxu_read32(priv,
  2785. REG_RX_POWER_AFTER_IQK_A_2);
  2786. result[t][3] = (val32 >> 16) & 0x3ff;
  2787. break;
  2788. } else if (i == (retry - 1) && path_a_ok == 0x01) {
  2789. /* TX IQK OK */
  2790. dev_dbg(dev, "%s: Path A IQK Only Tx Success!!\n",
  2791. __func__);
  2792. val32 = rtl8xxxu_read32(priv,
  2793. REG_TX_POWER_BEFORE_IQK_A);
  2794. result[t][0] = (val32 >> 16) & 0x3ff;
  2795. val32 = rtl8xxxu_read32(priv,
  2796. REG_TX_POWER_AFTER_IQK_A);
  2797. result[t][1] = (val32 >> 16) & 0x3ff;
  2798. }
  2799. }
  2800. if (!path_a_ok)
  2801. dev_dbg(dev, "%s: Path A IQK failed!\n", __func__);
  2802. if (priv->tx_paths > 1) {
  2803. /*
  2804. * Path A into standby
  2805. */
  2806. rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x0);
  2807. rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00010000);
  2808. rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0x80800000);
  2809. /* Turn Path B ADDA on */
  2810. rtl8xxxu_path_adda_on(priv, adda_regs, false);
  2811. for (i = 0; i < retry; i++) {
  2812. path_b_ok = rtl8xxxu_iqk_path_b(priv);
  2813. if (path_b_ok == 0x03) {
  2814. val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
  2815. result[t][4] = (val32 >> 16) & 0x3ff;
  2816. val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
  2817. result[t][5] = (val32 >> 16) & 0x3ff;
  2818. val32 = rtl8xxxu_read32(priv, REG_RX_POWER_BEFORE_IQK_B_2);
  2819. result[t][6] = (val32 >> 16) & 0x3ff;
  2820. val32 = rtl8xxxu_read32(priv, REG_RX_POWER_AFTER_IQK_B_2);
  2821. result[t][7] = (val32 >> 16) & 0x3ff;
  2822. break;
  2823. } else if (i == (retry - 1) && path_b_ok == 0x01) {
  2824. /* TX IQK OK */
  2825. val32 = rtl8xxxu_read32(priv, REG_TX_POWER_BEFORE_IQK_B);
  2826. result[t][4] = (val32 >> 16) & 0x3ff;
  2827. val32 = rtl8xxxu_read32(priv, REG_TX_POWER_AFTER_IQK_B);
  2828. result[t][5] = (val32 >> 16) & 0x3ff;
  2829. }
  2830. }
  2831. if (!path_b_ok)
  2832. dev_dbg(dev, "%s: Path B IQK failed!\n", __func__);
  2833. }
  2834. /* Back to BB mode, load original value */
  2835. rtl8xxxu_write32(priv, REG_FPGA0_IQK, 0);
  2836. if (t) {
  2837. if (!priv->pi_enabled) {
  2838. /*
  2839. * Switch back BB to SI mode after finishing
  2840. * IQ Calibration
  2841. */
  2842. val32 = 0x01000000;
  2843. rtl8xxxu_write32(priv, REG_FPGA0_XA_HSSI_PARM1, val32);
  2844. rtl8xxxu_write32(priv, REG_FPGA0_XB_HSSI_PARM1, val32);
  2845. }
  2846. /* Reload ADDA power saving parameters */
  2847. rtl8xxxu_restore_regs(priv, adda_regs, priv->adda_backup,
  2848. RTL8XXXU_ADDA_REGS);
  2849. /* Reload MAC parameters */
  2850. rtl8xxxu_restore_mac_regs(priv, iqk_mac_regs, priv->mac_backup);
  2851. /* Reload BB parameters */
  2852. rtl8xxxu_restore_regs(priv, iqk_bb_regs,
  2853. priv->bb_backup, RTL8XXXU_BB_REGS);
  2854. /* Restore RX initial gain */
  2855. rtl8xxxu_write32(priv, REG_FPGA0_XA_LSSI_PARM, 0x00032ed3);
  2856. if (priv->tx_paths > 1) {
  2857. rtl8xxxu_write32(priv, REG_FPGA0_XB_LSSI_PARM,
  2858. 0x00032ed3);
  2859. }
  2860. /* Load 0xe30 IQC default value */
  2861. rtl8xxxu_write32(priv, REG_TX_IQK_TONE_A, 0x01008c00);
  2862. rtl8xxxu_write32(priv, REG_RX_IQK_TONE_A, 0x01008c00);
  2863. }
  2864. }
  2865. void rtl8xxxu_gen2_prepare_calibrate(struct rtl8xxxu_priv *priv, u8 start)
  2866. {
  2867. struct h2c_cmd h2c;
  2868. memset(&h2c, 0, sizeof(struct h2c_cmd));
  2869. h2c.bt_wlan_calibration.cmd = H2C_8723B_BT_WLAN_CALIBRATION;
  2870. h2c.bt_wlan_calibration.data = start;
  2871. rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.bt_wlan_calibration));
  2872. }
  2873. void rtl8xxxu_gen1_phy_iq_calibrate(struct rtl8xxxu_priv *priv)
  2874. {
  2875. struct device *dev = &priv->udev->dev;
  2876. int result[4][8]; /* last is final result */
  2877. int i, candidate;
  2878. bool path_a_ok, path_b_ok;
  2879. u32 reg_e94, reg_e9c, reg_ea4, reg_eac;
  2880. u32 reg_eb4, reg_ebc, reg_ec4, reg_ecc;
  2881. s32 reg_tmp = 0;
  2882. bool simu;
  2883. memset(result, 0, sizeof(result));
  2884. candidate = -1;
  2885. path_a_ok = false;
  2886. path_b_ok = false;
  2887. rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  2888. for (i = 0; i < 3; i++) {
  2889. rtl8xxxu_phy_iqcalibrate(priv, result, i);
  2890. if (i == 1) {
  2891. simu = rtl8xxxu_simularity_compare(priv, result, 0, 1);
  2892. if (simu) {
  2893. candidate = 0;
  2894. break;
  2895. }
  2896. }
  2897. if (i == 2) {
  2898. simu = rtl8xxxu_simularity_compare(priv, result, 0, 2);
  2899. if (simu) {
  2900. candidate = 0;
  2901. break;
  2902. }
  2903. simu = rtl8xxxu_simularity_compare(priv, result, 1, 2);
  2904. if (simu) {
  2905. candidate = 1;
  2906. } else {
  2907. for (i = 0; i < 8; i++)
  2908. reg_tmp += result[3][i];
  2909. if (reg_tmp)
  2910. candidate = 3;
  2911. else
  2912. candidate = -1;
  2913. }
  2914. }
  2915. }
  2916. for (i = 0; i < 4; i++) {
  2917. reg_e94 = result[i][0];
  2918. reg_e9c = result[i][1];
  2919. reg_ea4 = result[i][2];
  2920. reg_eac = result[i][3];
  2921. reg_eb4 = result[i][4];
  2922. reg_ebc = result[i][5];
  2923. reg_ec4 = result[i][6];
  2924. reg_ecc = result[i][7];
  2925. }
  2926. if (candidate >= 0) {
  2927. reg_e94 = result[candidate][0];
  2928. priv->rege94 = reg_e94;
  2929. reg_e9c = result[candidate][1];
  2930. priv->rege9c = reg_e9c;
  2931. reg_ea4 = result[candidate][2];
  2932. reg_eac = result[candidate][3];
  2933. reg_eb4 = result[candidate][4];
  2934. priv->regeb4 = reg_eb4;
  2935. reg_ebc = result[candidate][5];
  2936. priv->regebc = reg_ebc;
  2937. reg_ec4 = result[candidate][6];
  2938. reg_ecc = result[candidate][7];
  2939. dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
  2940. dev_dbg(dev,
  2941. "%s: e94 =%x e9c=%x ea4=%x eac=%x eb4=%x ebc=%x ec4=%x ecc=%x\n",
  2942. __func__, reg_e94, reg_e9c,
  2943. reg_ea4, reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc);
  2944. path_a_ok = true;
  2945. path_b_ok = true;
  2946. } else {
  2947. reg_e94 = reg_eb4 = priv->rege94 = priv->regeb4 = 0x100;
  2948. reg_e9c = reg_ebc = priv->rege9c = priv->regebc = 0x0;
  2949. }
  2950. if (reg_e94 && candidate >= 0)
  2951. rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
  2952. candidate, (reg_ea4 == 0));
  2953. if (priv->tx_paths > 1 && reg_eb4)
  2954. rtl8xxxu_fill_iqk_matrix_b(priv, path_b_ok, result,
  2955. candidate, (reg_ec4 == 0));
  2956. rtl8xxxu_save_regs(priv, rtl8xxxu_iqk_phy_iq_bb_reg,
  2957. priv->bb_recovery_backup, RTL8XXXU_BB_REGS);
  2958. }
  2959. static void rtl8723a_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
  2960. {
  2961. u32 val32;
  2962. u32 rf_amode, rf_bmode = 0, lstf;
  2963. /* Check continuous TX and Packet TX */
  2964. lstf = rtl8xxxu_read32(priv, REG_OFDM1_LSTF);
  2965. if (lstf & OFDM_LSTF_MASK) {
  2966. /* Disable all continuous TX */
  2967. val32 = lstf & ~OFDM_LSTF_MASK;
  2968. rtl8xxxu_write32(priv, REG_OFDM1_LSTF, val32);
  2969. /* Read original RF mode Path A */
  2970. rf_amode = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_AC);
  2971. /* Set RF mode to standby Path A */
  2972. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC,
  2973. (rf_amode & 0x8ffff) | 0x10000);
  2974. /* Path-B */
  2975. if (priv->tx_paths > 1) {
  2976. rf_bmode = rtl8xxxu_read_rfreg(priv, RF_B,
  2977. RF6052_REG_AC);
  2978. rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
  2979. (rf_bmode & 0x8ffff) | 0x10000);
  2980. }
  2981. } else {
  2982. /* Deal with Packet TX case */
  2983. /* block all queues */
  2984. rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
  2985. }
  2986. /* Start LC calibration */
  2987. if (priv->fops->has_s0s1)
  2988. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdfbe0);
  2989. val32 = rtl8xxxu_read_rfreg(priv, RF_A, RF6052_REG_MODE_AG);
  2990. val32 |= 0x08000;
  2991. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_MODE_AG, val32);
  2992. msleep(100);
  2993. if (priv->fops->has_s0s1)
  2994. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_S0S1, 0xdffe0);
  2995. /* Restore original parameters */
  2996. if (lstf & OFDM_LSTF_MASK) {
  2997. /* Path-A */
  2998. rtl8xxxu_write32(priv, REG_OFDM1_LSTF, lstf);
  2999. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_AC, rf_amode);
  3000. /* Path-B */
  3001. if (priv->tx_paths > 1)
  3002. rtl8xxxu_write_rfreg(priv, RF_B, RF6052_REG_AC,
  3003. rf_bmode);
  3004. } else /* Deal with Packet TX case */
  3005. rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
  3006. }
  3007. static int rtl8xxxu_set_mac(struct rtl8xxxu_priv *priv)
  3008. {
  3009. int i;
  3010. u16 reg;
  3011. reg = REG_MACID;
  3012. for (i = 0; i < ETH_ALEN; i++)
  3013. rtl8xxxu_write8(priv, reg + i, priv->mac_addr[i]);
  3014. return 0;
  3015. }
  3016. static int rtl8xxxu_set_bssid(struct rtl8xxxu_priv *priv, const u8 *bssid)
  3017. {
  3018. int i;
  3019. u16 reg;
  3020. dev_dbg(&priv->udev->dev, "%s: (%pM)\n", __func__, bssid);
  3021. reg = REG_BSSID;
  3022. for (i = 0; i < ETH_ALEN; i++)
  3023. rtl8xxxu_write8(priv, reg + i, bssid[i]);
  3024. return 0;
  3025. }
  3026. static void
  3027. rtl8xxxu_set_ampdu_factor(struct rtl8xxxu_priv *priv, u8 ampdu_factor)
  3028. {
  3029. u8 vals[4] = { 0x41, 0xa8, 0x72, 0xb9 };
  3030. u8 max_agg = 0xf;
  3031. int i;
  3032. ampdu_factor = 1 << (ampdu_factor + 2);
  3033. if (ampdu_factor > max_agg)
  3034. ampdu_factor = max_agg;
  3035. for (i = 0; i < 4; i++) {
  3036. if ((vals[i] & 0xf0) > (ampdu_factor << 4))
  3037. vals[i] = (vals[i] & 0x0f) | (ampdu_factor << 4);
  3038. if ((vals[i] & 0x0f) > ampdu_factor)
  3039. vals[i] = (vals[i] & 0xf0) | ampdu_factor;
  3040. rtl8xxxu_write8(priv, REG_AGGLEN_LMT + i, vals[i]);
  3041. }
  3042. }
  3043. static void rtl8xxxu_set_ampdu_min_space(struct rtl8xxxu_priv *priv, u8 density)
  3044. {
  3045. u8 val8;
  3046. val8 = rtl8xxxu_read8(priv, REG_AMPDU_MIN_SPACE);
  3047. val8 &= 0xf8;
  3048. val8 |= density;
  3049. rtl8xxxu_write8(priv, REG_AMPDU_MIN_SPACE, val8);
  3050. }
  3051. static int rtl8xxxu_active_to_emu(struct rtl8xxxu_priv *priv)
  3052. {
  3053. u8 val8;
  3054. int count, ret = 0;
  3055. /* Start of rtl8723AU_card_enable_flow */
  3056. /* Act to Cardemu sequence*/
  3057. /* Turn off RF */
  3058. rtl8xxxu_write8(priv, REG_RF_CTRL, 0);
  3059. /* 0x004E[7] = 0, switch DPDT_SEL_P output from register 0x0065[2] */
  3060. val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
  3061. val8 &= ~LEDCFG2_DPDT_SELECT;
  3062. rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
  3063. /* 0x0005[1] = 1 turn off MAC by HW state machine*/
  3064. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3065. val8 |= BIT(1);
  3066. rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
  3067. for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
  3068. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3069. if ((val8 & BIT(1)) == 0)
  3070. break;
  3071. udelay(10);
  3072. }
  3073. if (!count) {
  3074. dev_warn(&priv->udev->dev, "%s: Disabling MAC timed out\n",
  3075. __func__);
  3076. ret = -EBUSY;
  3077. goto exit;
  3078. }
  3079. /* 0x0000[5] = 1 analog Ips to digital, 1:isolation */
  3080. val8 = rtl8xxxu_read8(priv, REG_SYS_ISO_CTRL);
  3081. val8 |= SYS_ISO_ANALOG_IPS;
  3082. rtl8xxxu_write8(priv, REG_SYS_ISO_CTRL, val8);
  3083. /* 0x0020[0] = 0 disable LDOA12 MACRO block*/
  3084. val8 = rtl8xxxu_read8(priv, REG_LDOA15_CTRL);
  3085. val8 &= ~LDOA15_ENABLE;
  3086. rtl8xxxu_write8(priv, REG_LDOA15_CTRL, val8);
  3087. exit:
  3088. return ret;
  3089. }
  3090. int rtl8xxxu_active_to_lps(struct rtl8xxxu_priv *priv)
  3091. {
  3092. u8 val8;
  3093. u8 val32;
  3094. int count, ret = 0;
  3095. rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
  3096. /*
  3097. * Poll - wait for RX packet to complete
  3098. */
  3099. for (count = RTL8XXXU_MAX_REG_POLL; count; count--) {
  3100. val32 = rtl8xxxu_read32(priv, 0x5f8);
  3101. if (!val32)
  3102. break;
  3103. udelay(10);
  3104. }
  3105. if (!count) {
  3106. dev_warn(&priv->udev->dev,
  3107. "%s: RX poll timed out (0x05f8)\n", __func__);
  3108. ret = -EBUSY;
  3109. goto exit;
  3110. }
  3111. /* Disable CCK and OFDM, clock gated */
  3112. val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
  3113. val8 &= ~SYS_FUNC_BBRSTB;
  3114. rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
  3115. udelay(2);
  3116. /* Reset baseband */
  3117. val8 = rtl8xxxu_read8(priv, REG_SYS_FUNC);
  3118. val8 &= ~SYS_FUNC_BB_GLB_RSTN;
  3119. rtl8xxxu_write8(priv, REG_SYS_FUNC, val8);
  3120. /* Reset MAC TRX */
  3121. val8 = rtl8xxxu_read8(priv, REG_CR);
  3122. val8 = CR_HCI_TXDMA_ENABLE | CR_HCI_RXDMA_ENABLE;
  3123. rtl8xxxu_write8(priv, REG_CR, val8);
  3124. /* Reset MAC TRX */
  3125. val8 = rtl8xxxu_read8(priv, REG_CR + 1);
  3126. val8 &= ~BIT(1); /* CR_SECURITY_ENABLE */
  3127. rtl8xxxu_write8(priv, REG_CR + 1, val8);
  3128. /* Respond TX OK to scheduler */
  3129. val8 = rtl8xxxu_read8(priv, REG_DUAL_TSF_RST);
  3130. val8 |= DUAL_TSF_TX_OK;
  3131. rtl8xxxu_write8(priv, REG_DUAL_TSF_RST, val8);
  3132. exit:
  3133. return ret;
  3134. }
  3135. void rtl8xxxu_disabled_to_emu(struct rtl8xxxu_priv *priv)
  3136. {
  3137. u8 val8;
  3138. /* Clear suspend enable and power down enable*/
  3139. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3140. val8 &= ~(BIT(3) | BIT(7));
  3141. rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
  3142. /* 0x48[16] = 0 to disable GPIO9 as EXT WAKEUP*/
  3143. val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
  3144. val8 &= ~BIT(0);
  3145. rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
  3146. /* 0x04[12:11] = 11 enable WL suspend*/
  3147. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3148. val8 &= ~(BIT(3) | BIT(4));
  3149. rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
  3150. }
  3151. static int rtl8xxxu_emu_to_disabled(struct rtl8xxxu_priv *priv)
  3152. {
  3153. u8 val8;
  3154. /* 0x0007[7:0] = 0x20 SOP option to disable BG/MB */
  3155. rtl8xxxu_write8(priv, REG_APS_FSMCO + 3, 0x20);
  3156. /* 0x04[12:11] = 01 enable WL suspend */
  3157. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3158. val8 &= ~BIT(4);
  3159. val8 |= BIT(3);
  3160. rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
  3161. val8 = rtl8xxxu_read8(priv, REG_APS_FSMCO + 1);
  3162. val8 |= BIT(7);
  3163. rtl8xxxu_write8(priv, REG_APS_FSMCO + 1, val8);
  3164. /* 0x48[16] = 1 to enable GPIO9 as EXT wakeup */
  3165. val8 = rtl8xxxu_read8(priv, REG_GPIO_INTM + 2);
  3166. val8 |= BIT(0);
  3167. rtl8xxxu_write8(priv, REG_GPIO_INTM + 2, val8);
  3168. return 0;
  3169. }
  3170. int rtl8xxxu_flush_fifo(struct rtl8xxxu_priv *priv)
  3171. {
  3172. struct device *dev = &priv->udev->dev;
  3173. u32 val32;
  3174. int retry, retval;
  3175. rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
  3176. val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
  3177. val32 |= RXPKT_NUM_RW_RELEASE_EN;
  3178. rtl8xxxu_write32(priv, REG_RXPKT_NUM, val32);
  3179. retry = 100;
  3180. retval = -EBUSY;
  3181. do {
  3182. val32 = rtl8xxxu_read32(priv, REG_RXPKT_NUM);
  3183. if (val32 & RXPKT_NUM_RXDMA_IDLE) {
  3184. retval = 0;
  3185. break;
  3186. }
  3187. } while (retry--);
  3188. rtl8xxxu_write16(priv, REG_RQPN_NPQ, 0);
  3189. rtl8xxxu_write32(priv, REG_RQPN, 0x80000000);
  3190. mdelay(2);
  3191. if (!retry)
  3192. dev_warn(dev, "Failed to flush FIFO\n");
  3193. return retval;
  3194. }
  3195. void rtl8xxxu_gen1_usb_quirks(struct rtl8xxxu_priv *priv)
  3196. {
  3197. /* Fix USB interface interference issue */
  3198. rtl8xxxu_write8(priv, 0xfe40, 0xe0);
  3199. rtl8xxxu_write8(priv, 0xfe41, 0x8d);
  3200. rtl8xxxu_write8(priv, 0xfe42, 0x80);
  3201. /*
  3202. * This sets TXDMA_OFFSET_DROP_DATA_EN (bit 9) as well as bits
  3203. * 8 and 5, for which I have found no documentation.
  3204. */
  3205. rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, 0xfd0320);
  3206. /*
  3207. * Solve too many protocol error on USB bus.
  3208. * Can't do this for 8188/8192 UMC A cut parts
  3209. */
  3210. if (!(!priv->chip_cut && priv->vendor_umc)) {
  3211. rtl8xxxu_write8(priv, 0xfe40, 0xe6);
  3212. rtl8xxxu_write8(priv, 0xfe41, 0x94);
  3213. rtl8xxxu_write8(priv, 0xfe42, 0x80);
  3214. rtl8xxxu_write8(priv, 0xfe40, 0xe0);
  3215. rtl8xxxu_write8(priv, 0xfe41, 0x19);
  3216. rtl8xxxu_write8(priv, 0xfe42, 0x80);
  3217. rtl8xxxu_write8(priv, 0xfe40, 0xe5);
  3218. rtl8xxxu_write8(priv, 0xfe41, 0x91);
  3219. rtl8xxxu_write8(priv, 0xfe42, 0x80);
  3220. rtl8xxxu_write8(priv, 0xfe40, 0xe2);
  3221. rtl8xxxu_write8(priv, 0xfe41, 0x81);
  3222. rtl8xxxu_write8(priv, 0xfe42, 0x80);
  3223. }
  3224. }
  3225. void rtl8xxxu_gen2_usb_quirks(struct rtl8xxxu_priv *priv)
  3226. {
  3227. u32 val32;
  3228. val32 = rtl8xxxu_read32(priv, REG_TXDMA_OFFSET_CHK);
  3229. val32 |= TXDMA_OFFSET_DROP_DATA_EN;
  3230. rtl8xxxu_write32(priv, REG_TXDMA_OFFSET_CHK, val32);
  3231. }
  3232. void rtl8xxxu_power_off(struct rtl8xxxu_priv *priv)
  3233. {
  3234. u8 val8;
  3235. u16 val16;
  3236. u32 val32;
  3237. /*
  3238. * Workaround for 8188RU LNA power leakage problem.
  3239. */
  3240. if (priv->rtl_chip == RTL8188R) {
  3241. val32 = rtl8xxxu_read32(priv, REG_FPGA0_XCD_RF_PARM);
  3242. val32 |= BIT(1);
  3243. rtl8xxxu_write32(priv, REG_FPGA0_XCD_RF_PARM, val32);
  3244. }
  3245. rtl8xxxu_flush_fifo(priv);
  3246. rtl8xxxu_active_to_lps(priv);
  3247. /* Turn off RF */
  3248. rtl8xxxu_write8(priv, REG_RF_CTRL, 0x00);
  3249. /* Reset Firmware if running in RAM */
  3250. if (rtl8xxxu_read8(priv, REG_MCU_FW_DL) & MCU_FW_RAM_SEL)
  3251. rtl8xxxu_firmware_self_reset(priv);
  3252. /* Reset MCU */
  3253. val16 = rtl8xxxu_read16(priv, REG_SYS_FUNC);
  3254. val16 &= ~SYS_FUNC_CPU_ENABLE;
  3255. rtl8xxxu_write16(priv, REG_SYS_FUNC, val16);
  3256. /* Reset MCU ready status */
  3257. rtl8xxxu_write8(priv, REG_MCU_FW_DL, 0x00);
  3258. rtl8xxxu_active_to_emu(priv);
  3259. rtl8xxxu_emu_to_disabled(priv);
  3260. /* Reset MCU IO Wrapper */
  3261. val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
  3262. val8 &= ~BIT(0);
  3263. rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
  3264. val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL + 1);
  3265. val8 |= BIT(0);
  3266. rtl8xxxu_write8(priv, REG_RSV_CTRL + 1, val8);
  3267. /* RSV_CTRL 0x1C[7:0] = 0x0e lock ISO/CLK/Power control register */
  3268. rtl8xxxu_write8(priv, REG_RSV_CTRL, 0x0e);
  3269. }
  3270. #ifdef NEED_PS_TDMA
  3271. static void rtl8723bu_set_ps_tdma(struct rtl8xxxu_priv *priv,
  3272. u8 arg1, u8 arg2, u8 arg3, u8 arg4, u8 arg5)
  3273. {
  3274. struct h2c_cmd h2c;
  3275. memset(&h2c, 0, sizeof(struct h2c_cmd));
  3276. h2c.b_type_dma.cmd = H2C_8723B_B_TYPE_TDMA;
  3277. h2c.b_type_dma.data1 = arg1;
  3278. h2c.b_type_dma.data2 = arg2;
  3279. h2c.b_type_dma.data3 = arg3;
  3280. h2c.b_type_dma.data4 = arg4;
  3281. h2c.b_type_dma.data5 = arg5;
  3282. rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_type_dma));
  3283. }
  3284. #endif
  3285. void rtl8xxxu_gen2_disable_rf(struct rtl8xxxu_priv *priv)
  3286. {
  3287. u32 val32;
  3288. val32 = rtl8xxxu_read32(priv, REG_RX_WAIT_CCA);
  3289. val32 &= ~(BIT(22) | BIT(23));
  3290. rtl8xxxu_write32(priv, REG_RX_WAIT_CCA, val32);
  3291. }
  3292. static void rtl8xxxu_init_queue_reserved_page(struct rtl8xxxu_priv *priv)
  3293. {
  3294. struct rtl8xxxu_fileops *fops = priv->fops;
  3295. u32 hq, lq, nq, eq, pubq;
  3296. u32 val32;
  3297. hq = 0;
  3298. lq = 0;
  3299. nq = 0;
  3300. eq = 0;
  3301. pubq = 0;
  3302. if (priv->ep_tx_high_queue)
  3303. hq = fops->page_num_hi;
  3304. if (priv->ep_tx_low_queue)
  3305. lq = fops->page_num_lo;
  3306. if (priv->ep_tx_normal_queue)
  3307. nq = fops->page_num_norm;
  3308. val32 = (nq << RQPN_NPQ_SHIFT) | (eq << RQPN_EPQ_SHIFT);
  3309. rtl8xxxu_write32(priv, REG_RQPN_NPQ, val32);
  3310. pubq = fops->total_page_num - hq - lq - nq - 1;
  3311. val32 = RQPN_LOAD;
  3312. val32 |= (hq << RQPN_HI_PQ_SHIFT);
  3313. val32 |= (lq << RQPN_LO_PQ_SHIFT);
  3314. val32 |= (pubq << RQPN_PUB_PQ_SHIFT);
  3315. rtl8xxxu_write32(priv, REG_RQPN, val32);
  3316. }
  3317. static int rtl8xxxu_init_device(struct ieee80211_hw *hw)
  3318. {
  3319. struct rtl8xxxu_priv *priv = hw->priv;
  3320. struct device *dev = &priv->udev->dev;
  3321. struct rtl8xxxu_fileops *fops = priv->fops;
  3322. bool macpower;
  3323. int ret;
  3324. u8 val8;
  3325. u16 val16;
  3326. u32 val32;
  3327. /* Check if MAC is already powered on */
  3328. val8 = rtl8xxxu_read8(priv, REG_CR);
  3329. /*
  3330. * Fix 92DU-VC S3 hang with the reason is that secondary mac is not
  3331. * initialized. First MAC returns 0xea, second MAC returns 0x00
  3332. */
  3333. if (val8 == 0xea)
  3334. macpower = false;
  3335. else
  3336. macpower = true;
  3337. if (fops->needs_full_init)
  3338. macpower = false;
  3339. ret = fops->power_on(priv);
  3340. if (ret < 0) {
  3341. dev_warn(dev, "%s: Failed power on\n", __func__);
  3342. goto exit;
  3343. }
  3344. if (!macpower)
  3345. rtl8xxxu_init_queue_reserved_page(priv);
  3346. ret = rtl8xxxu_init_queue_priority(priv);
  3347. dev_dbg(dev, "%s: init_queue_priority %i\n", __func__, ret);
  3348. if (ret)
  3349. goto exit;
  3350. /*
  3351. * Set RX page boundary
  3352. */
  3353. rtl8xxxu_write16(priv, REG_TRXFF_BNDY + 2, fops->trxff_boundary);
  3354. ret = rtl8xxxu_download_firmware(priv);
  3355. dev_dbg(dev, "%s: download_firmware %i\n", __func__, ret);
  3356. if (ret)
  3357. goto exit;
  3358. ret = rtl8xxxu_start_firmware(priv);
  3359. dev_dbg(dev, "%s: start_firmware %i\n", __func__, ret);
  3360. if (ret)
  3361. goto exit;
  3362. if (fops->phy_init_antenna_selection)
  3363. fops->phy_init_antenna_selection(priv);
  3364. ret = rtl8xxxu_init_mac(priv);
  3365. dev_dbg(dev, "%s: init_mac %i\n", __func__, ret);
  3366. if (ret)
  3367. goto exit;
  3368. ret = rtl8xxxu_init_phy_bb(priv);
  3369. dev_dbg(dev, "%s: init_phy_bb %i\n", __func__, ret);
  3370. if (ret)
  3371. goto exit;
  3372. ret = fops->init_phy_rf(priv);
  3373. if (ret)
  3374. goto exit;
  3375. /* RFSW Control - clear bit 14 ?? */
  3376. if (priv->rtl_chip != RTL8723B && priv->rtl_chip != RTL8192E)
  3377. rtl8xxxu_write32(priv, REG_FPGA0_TX_INFO, 0x00000003);
  3378. val32 = FPGA0_RF_TRSW | FPGA0_RF_TRSWB | FPGA0_RF_ANTSW |
  3379. FPGA0_RF_ANTSWB |
  3380. ((FPGA0_RF_ANTSW | FPGA0_RF_ANTSWB) << FPGA0_RF_BD_CTRL_SHIFT);
  3381. if (!priv->no_pape) {
  3382. val32 |= (FPGA0_RF_PAPE |
  3383. (FPGA0_RF_PAPE << FPGA0_RF_BD_CTRL_SHIFT));
  3384. }
  3385. rtl8xxxu_write32(priv, REG_FPGA0_XAB_RF_SW_CTRL, val32);
  3386. /* 0x860[6:5]= 00 - why? - this sets antenna B */
  3387. if (priv->rtl_chip != RTL8192E)
  3388. rtl8xxxu_write32(priv, REG_FPGA0_XA_RF_INT_OE, 0x66f60210);
  3389. if (!macpower) {
  3390. /*
  3391. * Set TX buffer boundary
  3392. */
  3393. val8 = fops->total_page_num + 1;
  3394. rtl8xxxu_write8(priv, REG_TXPKTBUF_BCNQ_BDNY, val8);
  3395. rtl8xxxu_write8(priv, REG_TXPKTBUF_MGQ_BDNY, val8);
  3396. rtl8xxxu_write8(priv, REG_TXPKTBUF_WMAC_LBK_BF_HD, val8);
  3397. rtl8xxxu_write8(priv, REG_TRXFF_BNDY, val8);
  3398. rtl8xxxu_write8(priv, REG_TDECTRL + 1, val8);
  3399. }
  3400. /*
  3401. * The vendor drivers set PBP for all devices, except 8192e.
  3402. * There is no explanation for this in any of the sources.
  3403. */
  3404. val8 = (fops->pbp_rx << PBP_PAGE_SIZE_RX_SHIFT) |
  3405. (fops->pbp_tx << PBP_PAGE_SIZE_TX_SHIFT);
  3406. if (priv->rtl_chip != RTL8192E)
  3407. rtl8xxxu_write8(priv, REG_PBP, val8);
  3408. dev_dbg(dev, "%s: macpower %i\n", __func__, macpower);
  3409. if (!macpower) {
  3410. ret = fops->llt_init(priv);
  3411. if (ret) {
  3412. dev_warn(dev, "%s: LLT table init failed\n", __func__);
  3413. goto exit;
  3414. }
  3415. /*
  3416. * Chip specific quirks
  3417. */
  3418. fops->usb_quirks(priv);
  3419. /*
  3420. * Enable TX report and TX report timer for 8723bu/8188eu/...
  3421. */
  3422. if (fops->has_tx_report) {
  3423. val8 = rtl8xxxu_read8(priv, REG_TX_REPORT_CTRL);
  3424. val8 |= TX_REPORT_CTRL_TIMER_ENABLE;
  3425. rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL, val8);
  3426. /* Set MAX RPT MACID */
  3427. rtl8xxxu_write8(priv, REG_TX_REPORT_CTRL + 1, 0x02);
  3428. /* TX report Timer. Unit: 32us */
  3429. rtl8xxxu_write16(priv, REG_TX_REPORT_TIME, 0xcdf0);
  3430. /* tmp ps ? */
  3431. val8 = rtl8xxxu_read8(priv, 0xa3);
  3432. val8 &= 0xf8;
  3433. rtl8xxxu_write8(priv, 0xa3, val8);
  3434. }
  3435. }
  3436. /*
  3437. * Unit in 8 bytes, not obvious what it is used for
  3438. */
  3439. rtl8xxxu_write8(priv, REG_RX_DRVINFO_SZ, 4);
  3440. if (priv->rtl_chip == RTL8192E) {
  3441. rtl8xxxu_write32(priv, REG_HIMR0, 0x00);
  3442. rtl8xxxu_write32(priv, REG_HIMR1, 0x00);
  3443. } else {
  3444. /*
  3445. * Enable all interrupts - not obvious USB needs to do this
  3446. */
  3447. rtl8xxxu_write32(priv, REG_HISR, 0xffffffff);
  3448. rtl8xxxu_write32(priv, REG_HIMR, 0xffffffff);
  3449. }
  3450. rtl8xxxu_set_mac(priv);
  3451. rtl8xxxu_set_linktype(priv, NL80211_IFTYPE_STATION);
  3452. /*
  3453. * Configure initial WMAC settings
  3454. */
  3455. val32 = RCR_ACCEPT_PHYS_MATCH | RCR_ACCEPT_MCAST | RCR_ACCEPT_BCAST |
  3456. RCR_ACCEPT_MGMT_FRAME | RCR_HTC_LOC_CTRL |
  3457. RCR_APPEND_PHYSTAT | RCR_APPEND_ICV | RCR_APPEND_MIC;
  3458. rtl8xxxu_write32(priv, REG_RCR, val32);
  3459. /*
  3460. * Accept all multicast
  3461. */
  3462. rtl8xxxu_write32(priv, REG_MAR, 0xffffffff);
  3463. rtl8xxxu_write32(priv, REG_MAR + 4, 0xffffffff);
  3464. /*
  3465. * Init adaptive controls
  3466. */
  3467. val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
  3468. val32 &= ~RESPONSE_RATE_BITMAP_ALL;
  3469. val32 |= RESPONSE_RATE_RRSR_CCK_ONLY_1M;
  3470. rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
  3471. /* CCK = 0x0a, OFDM = 0x10 */
  3472. rtl8xxxu_set_spec_sifs(priv, 0x10, 0x10);
  3473. rtl8xxxu_set_retry(priv, 0x30, 0x30);
  3474. rtl8xxxu_set_spec_sifs(priv, 0x0a, 0x10);
  3475. /*
  3476. * Init EDCA
  3477. */
  3478. rtl8xxxu_write16(priv, REG_MAC_SPEC_SIFS, 0x100a);
  3479. /* Set CCK SIFS */
  3480. rtl8xxxu_write16(priv, REG_SIFS_CCK, 0x100a);
  3481. /* Set OFDM SIFS */
  3482. rtl8xxxu_write16(priv, REG_SIFS_OFDM, 0x100a);
  3483. /* TXOP */
  3484. rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, 0x005ea42b);
  3485. rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, 0x0000a44f);
  3486. rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, 0x005ea324);
  3487. rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, 0x002fa226);
  3488. /* Set data auto rate fallback retry count */
  3489. rtl8xxxu_write32(priv, REG_DARFRC, 0x00000000);
  3490. rtl8xxxu_write32(priv, REG_DARFRC + 4, 0x10080404);
  3491. rtl8xxxu_write32(priv, REG_RARFRC, 0x04030201);
  3492. rtl8xxxu_write32(priv, REG_RARFRC + 4, 0x08070605);
  3493. val8 = rtl8xxxu_read8(priv, REG_FWHW_TXQ_CTRL);
  3494. val8 |= FWHW_TXQ_CTRL_AMPDU_RETRY;
  3495. rtl8xxxu_write8(priv, REG_FWHW_TXQ_CTRL, val8);
  3496. /* Set ACK timeout */
  3497. rtl8xxxu_write8(priv, REG_ACKTO, 0x40);
  3498. /*
  3499. * Initialize beacon parameters
  3500. */
  3501. val16 = BEACON_DISABLE_TSF_UPDATE | (BEACON_DISABLE_TSF_UPDATE << 8);
  3502. rtl8xxxu_write16(priv, REG_BEACON_CTRL, val16);
  3503. rtl8xxxu_write16(priv, REG_TBTT_PROHIBIT, 0x6404);
  3504. rtl8xxxu_write8(priv, REG_DRIVER_EARLY_INT, DRIVER_EARLY_INT_TIME);
  3505. rtl8xxxu_write8(priv, REG_BEACON_DMA_TIME, BEACON_DMA_ATIME_INT_TIME);
  3506. rtl8xxxu_write16(priv, REG_BEACON_TCFG, 0x660F);
  3507. /*
  3508. * Initialize burst parameters
  3509. */
  3510. if (priv->rtl_chip == RTL8723B) {
  3511. /*
  3512. * For USB high speed set 512B packets
  3513. */
  3514. val8 = rtl8xxxu_read8(priv, REG_RXDMA_PRO_8723B);
  3515. val8 &= ~(BIT(4) | BIT(5));
  3516. val8 |= BIT(4);
  3517. val8 |= BIT(1) | BIT(2) | BIT(3);
  3518. rtl8xxxu_write8(priv, REG_RXDMA_PRO_8723B, val8);
  3519. /*
  3520. * For USB high speed set 512B packets
  3521. */
  3522. val8 = rtl8xxxu_read8(priv, REG_HT_SINGLE_AMPDU_8723B);
  3523. val8 |= BIT(7);
  3524. rtl8xxxu_write8(priv, REG_HT_SINGLE_AMPDU_8723B, val8);
  3525. rtl8xxxu_write16(priv, REG_MAX_AGGR_NUM, 0x0c14);
  3526. rtl8xxxu_write8(priv, REG_AMPDU_MAX_TIME_8723B, 0x5e);
  3527. rtl8xxxu_write32(priv, REG_AGGLEN_LMT, 0xffffffff);
  3528. rtl8xxxu_write8(priv, REG_RX_PKT_LIMIT, 0x18);
  3529. rtl8xxxu_write8(priv, REG_PIFS, 0x00);
  3530. rtl8xxxu_write8(priv, REG_USTIME_TSF_8723B, 0x50);
  3531. rtl8xxxu_write8(priv, REG_USTIME_EDCA, 0x50);
  3532. val8 = rtl8xxxu_read8(priv, REG_RSV_CTRL);
  3533. val8 |= BIT(5) | BIT(6);
  3534. rtl8xxxu_write8(priv, REG_RSV_CTRL, val8);
  3535. }
  3536. if (fops->init_aggregation)
  3537. fops->init_aggregation(priv);
  3538. /*
  3539. * Enable CCK and OFDM block
  3540. */
  3541. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  3542. val32 |= (FPGA_RF_MODE_CCK | FPGA_RF_MODE_OFDM);
  3543. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  3544. /*
  3545. * Invalidate all CAM entries - bit 30 is undocumented
  3546. */
  3547. rtl8xxxu_write32(priv, REG_CAM_CMD, CAM_CMD_POLLING | BIT(30));
  3548. /*
  3549. * Start out with default power levels for channel 6, 20MHz
  3550. */
  3551. fops->set_tx_power(priv, 1, false);
  3552. /* Let the 8051 take control of antenna setting */
  3553. if (priv->rtl_chip != RTL8192E) {
  3554. val8 = rtl8xxxu_read8(priv, REG_LEDCFG2);
  3555. val8 |= LEDCFG2_DPDT_SELECT;
  3556. rtl8xxxu_write8(priv, REG_LEDCFG2, val8);
  3557. }
  3558. rtl8xxxu_write8(priv, REG_HWSEQ_CTRL, 0xff);
  3559. /* Disable BAR - not sure if this has any effect on USB */
  3560. rtl8xxxu_write32(priv, REG_BAR_MODE_CTRL, 0x0201ffff);
  3561. rtl8xxxu_write16(priv, REG_FAST_EDCA_CTRL, 0);
  3562. if (fops->init_statistics)
  3563. fops->init_statistics(priv);
  3564. if (priv->rtl_chip == RTL8192E) {
  3565. /*
  3566. * 0x4c6[3] 1: RTS BW = Data BW
  3567. * 0: RTS BW depends on CCA / secondary CCA result.
  3568. */
  3569. val8 = rtl8xxxu_read8(priv, REG_QUEUE_CTRL);
  3570. val8 &= ~BIT(3);
  3571. rtl8xxxu_write8(priv, REG_QUEUE_CTRL, val8);
  3572. /*
  3573. * Reset USB mode switch setting
  3574. */
  3575. rtl8xxxu_write8(priv, REG_ACLK_MON, 0x00);
  3576. }
  3577. rtl8723a_phy_lc_calibrate(priv);
  3578. fops->phy_iq_calibrate(priv);
  3579. /*
  3580. * This should enable thermal meter
  3581. */
  3582. if (fops->gen2_thermal_meter)
  3583. rtl8xxxu_write_rfreg(priv,
  3584. RF_A, RF6052_REG_T_METER_8723B, 0x37cf8);
  3585. else
  3586. rtl8xxxu_write_rfreg(priv, RF_A, RF6052_REG_T_METER, 0x60);
  3587. /* Set NAV_UPPER to 30000us */
  3588. val8 = ((30000 + NAV_UPPER_UNIT - 1) / NAV_UPPER_UNIT);
  3589. rtl8xxxu_write8(priv, REG_NAV_UPPER, val8);
  3590. if (priv->rtl_chip == RTL8723A) {
  3591. /*
  3592. * 2011/03/09 MH debug only, UMC-B cut pass 2500 S5 test,
  3593. * but we need to find root cause.
  3594. * This is 8723au only.
  3595. */
  3596. val32 = rtl8xxxu_read32(priv, REG_FPGA0_RF_MODE);
  3597. if ((val32 & 0xff000000) != 0x83000000) {
  3598. val32 |= FPGA_RF_MODE_CCK;
  3599. rtl8xxxu_write32(priv, REG_FPGA0_RF_MODE, val32);
  3600. }
  3601. } else if (priv->rtl_chip == RTL8192E) {
  3602. rtl8xxxu_write8(priv, REG_USB_HRPWM, 0x00);
  3603. }
  3604. val32 = rtl8xxxu_read32(priv, REG_FWHW_TXQ_CTRL);
  3605. val32 |= FWHW_TXQ_CTRL_XMIT_MGMT_ACK;
  3606. /* ack for xmit mgmt frames. */
  3607. rtl8xxxu_write32(priv, REG_FWHW_TXQ_CTRL, val32);
  3608. if (priv->rtl_chip == RTL8192E) {
  3609. /*
  3610. * Fix LDPC rx hang issue.
  3611. */
  3612. val32 = rtl8xxxu_read32(priv, REG_AFE_MISC);
  3613. rtl8xxxu_write8(priv, REG_8192E_LDOV12_CTRL, 0x75);
  3614. val32 &= 0xfff00fff;
  3615. val32 |= 0x0007e000;
  3616. rtl8xxxu_write32(priv, REG_AFE_MISC, val32);
  3617. }
  3618. exit:
  3619. return ret;
  3620. }
  3621. static void rtl8xxxu_cam_write(struct rtl8xxxu_priv *priv,
  3622. struct ieee80211_key_conf *key, const u8 *mac)
  3623. {
  3624. u32 cmd, val32, addr, ctrl;
  3625. int j, i, tmp_debug;
  3626. tmp_debug = rtl8xxxu_debug;
  3627. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_KEY)
  3628. rtl8xxxu_debug |= RTL8XXXU_DEBUG_REG_WRITE;
  3629. /*
  3630. * This is a bit of a hack - the lower bits of the cipher
  3631. * suite selector happens to match the cipher index in the CAM
  3632. */
  3633. addr = key->keyidx << CAM_CMD_KEY_SHIFT;
  3634. ctrl = (key->cipher & 0x0f) << 2 | key->keyidx | CAM_WRITE_VALID;
  3635. for (j = 5; j >= 0; j--) {
  3636. switch (j) {
  3637. case 0:
  3638. val32 = ctrl | (mac[0] << 16) | (mac[1] << 24);
  3639. break;
  3640. case 1:
  3641. val32 = mac[2] | (mac[3] << 8) |
  3642. (mac[4] << 16) | (mac[5] << 24);
  3643. break;
  3644. default:
  3645. i = (j - 2) << 2;
  3646. val32 = key->key[i] | (key->key[i + 1] << 8) |
  3647. key->key[i + 2] << 16 | key->key[i + 3] << 24;
  3648. break;
  3649. }
  3650. rtl8xxxu_write32(priv, REG_CAM_WRITE, val32);
  3651. cmd = CAM_CMD_POLLING | CAM_CMD_WRITE | (addr + j);
  3652. rtl8xxxu_write32(priv, REG_CAM_CMD, cmd);
  3653. udelay(100);
  3654. }
  3655. rtl8xxxu_debug = tmp_debug;
  3656. }
  3657. static void rtl8xxxu_sw_scan_start(struct ieee80211_hw *hw,
  3658. struct ieee80211_vif *vif, const u8 *mac)
  3659. {
  3660. struct rtl8xxxu_priv *priv = hw->priv;
  3661. u8 val8;
  3662. val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
  3663. val8 |= BEACON_DISABLE_TSF_UPDATE;
  3664. rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
  3665. }
  3666. static void rtl8xxxu_sw_scan_complete(struct ieee80211_hw *hw,
  3667. struct ieee80211_vif *vif)
  3668. {
  3669. struct rtl8xxxu_priv *priv = hw->priv;
  3670. u8 val8;
  3671. val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
  3672. val8 &= ~BEACON_DISABLE_TSF_UPDATE;
  3673. rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
  3674. }
  3675. void rtl8xxxu_update_rate_mask(struct rtl8xxxu_priv *priv, u32 ramask, int sgi)
  3676. {
  3677. struct h2c_cmd h2c;
  3678. memset(&h2c, 0, sizeof(struct h2c_cmd));
  3679. h2c.ramask.cmd = H2C_SET_RATE_MASK;
  3680. h2c.ramask.mask_lo = cpu_to_le16(ramask & 0xffff);
  3681. h2c.ramask.mask_hi = cpu_to_le16(ramask >> 16);
  3682. h2c.ramask.arg = 0x80;
  3683. if (sgi)
  3684. h2c.ramask.arg |= 0x20;
  3685. dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
  3686. __func__, ramask, h2c.ramask.arg, sizeof(h2c.ramask));
  3687. rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.ramask));
  3688. }
  3689. void rtl8xxxu_gen2_update_rate_mask(struct rtl8xxxu_priv *priv,
  3690. u32 ramask, int sgi)
  3691. {
  3692. struct h2c_cmd h2c;
  3693. u8 bw = 0;
  3694. memset(&h2c, 0, sizeof(struct h2c_cmd));
  3695. h2c.b_macid_cfg.cmd = H2C_8723B_MACID_CFG_RAID;
  3696. h2c.b_macid_cfg.ramask0 = ramask & 0xff;
  3697. h2c.b_macid_cfg.ramask1 = (ramask >> 8) & 0xff;
  3698. h2c.b_macid_cfg.ramask2 = (ramask >> 16) & 0xff;
  3699. h2c.b_macid_cfg.ramask3 = (ramask >> 24) & 0xff;
  3700. h2c.ramask.arg = 0x80;
  3701. h2c.b_macid_cfg.data1 = 0;
  3702. if (sgi)
  3703. h2c.b_macid_cfg.data1 |= BIT(7);
  3704. h2c.b_macid_cfg.data2 = bw;
  3705. dev_dbg(&priv->udev->dev, "%s: rate mask %08x, arg %02x, size %zi\n",
  3706. __func__, ramask, h2c.ramask.arg, sizeof(h2c.b_macid_cfg));
  3707. rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.b_macid_cfg));
  3708. }
  3709. void rtl8xxxu_gen1_report_connect(struct rtl8xxxu_priv *priv,
  3710. u8 macid, bool connect)
  3711. {
  3712. struct h2c_cmd h2c;
  3713. memset(&h2c, 0, sizeof(struct h2c_cmd));
  3714. h2c.joinbss.cmd = H2C_JOIN_BSS_REPORT;
  3715. if (connect)
  3716. h2c.joinbss.data = H2C_JOIN_BSS_CONNECT;
  3717. else
  3718. h2c.joinbss.data = H2C_JOIN_BSS_DISCONNECT;
  3719. rtl8xxxu_gen1_h2c_cmd(priv, &h2c, sizeof(h2c.joinbss));
  3720. }
  3721. void rtl8xxxu_gen2_report_connect(struct rtl8xxxu_priv *priv,
  3722. u8 macid, bool connect)
  3723. {
  3724. #ifdef RTL8XXXU_GEN2_REPORT_CONNECT
  3725. /*
  3726. * Barry Day reports this causes issues with 8192eu and 8723bu
  3727. * devices reconnecting. The reason for this is unclear, but
  3728. * until it is better understood, leave the code in place but
  3729. * disabled, so it is not lost.
  3730. */
  3731. struct h2c_cmd h2c;
  3732. memset(&h2c, 0, sizeof(struct h2c_cmd));
  3733. h2c.media_status_rpt.cmd = H2C_8723B_MEDIA_STATUS_RPT;
  3734. if (connect)
  3735. h2c.media_status_rpt.parm |= BIT(0);
  3736. else
  3737. h2c.media_status_rpt.parm &= ~BIT(0);
  3738. rtl8xxxu_gen2_h2c_cmd(priv, &h2c, sizeof(h2c.media_status_rpt));
  3739. #endif
  3740. }
  3741. void rtl8xxxu_gen1_init_aggregation(struct rtl8xxxu_priv *priv)
  3742. {
  3743. u8 agg_ctrl, usb_spec, page_thresh, timeout;
  3744. usb_spec = rtl8xxxu_read8(priv, REG_USB_SPECIAL_OPTION);
  3745. usb_spec &= ~USB_SPEC_USB_AGG_ENABLE;
  3746. rtl8xxxu_write8(priv, REG_USB_SPECIAL_OPTION, usb_spec);
  3747. agg_ctrl = rtl8xxxu_read8(priv, REG_TRXDMA_CTRL);
  3748. agg_ctrl &= ~TRXDMA_CTRL_RXDMA_AGG_EN;
  3749. if (!rtl8xxxu_dma_aggregation) {
  3750. rtl8xxxu_write8(priv, REG_TRXDMA_CTRL, agg_ctrl);
  3751. return;
  3752. }
  3753. agg_ctrl |= TRXDMA_CTRL_RXDMA_AGG_EN;
  3754. rtl8xxxu_write8(priv, REG_TRXDMA_CTRL, agg_ctrl);
  3755. /*
  3756. * The number of packets we can take looks to be buffer size / 512
  3757. * which matches the 512 byte rounding we have to do when de-muxing
  3758. * the packets.
  3759. *
  3760. * Sample numbers from the vendor driver:
  3761. * USB High-Speed mode values:
  3762. * RxAggBlockCount = 8 : 512 byte unit
  3763. * RxAggBlockTimeout = 6
  3764. * RxAggPageCount = 48 : 128 byte unit
  3765. * RxAggPageTimeout = 4 or 6 (absolute time 34ms/(2^6))
  3766. */
  3767. page_thresh = (priv->fops->rx_agg_buf_size / 512);
  3768. if (rtl8xxxu_dma_agg_pages >= 0) {
  3769. if (rtl8xxxu_dma_agg_pages <= page_thresh)
  3770. timeout = page_thresh;
  3771. else if (rtl8xxxu_dma_agg_pages <= 6)
  3772. dev_err(&priv->udev->dev,
  3773. "%s: dma_agg_pages=%i too small, minimum is 6\n",
  3774. __func__, rtl8xxxu_dma_agg_pages);
  3775. else
  3776. dev_err(&priv->udev->dev,
  3777. "%s: dma_agg_pages=%i larger than limit %i\n",
  3778. __func__, rtl8xxxu_dma_agg_pages, page_thresh);
  3779. }
  3780. rtl8xxxu_write8(priv, REG_RXDMA_AGG_PG_TH, page_thresh);
  3781. /*
  3782. * REG_RXDMA_AGG_PG_TH + 1 seems to be the timeout register on
  3783. * gen2 chips and rtl8188eu. The rtl8723au seems unhappy if we
  3784. * don't set it, so better set both.
  3785. */
  3786. timeout = 4;
  3787. if (rtl8xxxu_dma_agg_timeout >= 0) {
  3788. if (rtl8xxxu_dma_agg_timeout <= 127)
  3789. timeout = rtl8xxxu_dma_agg_timeout;
  3790. else
  3791. dev_err(&priv->udev->dev,
  3792. "%s: Invalid dma_agg_timeout: %i\n",
  3793. __func__, rtl8xxxu_dma_agg_timeout);
  3794. }
  3795. rtl8xxxu_write8(priv, REG_RXDMA_AGG_PG_TH + 1, timeout);
  3796. rtl8xxxu_write8(priv, REG_USB_DMA_AGG_TO, timeout);
  3797. priv->rx_buf_aggregation = 1;
  3798. }
  3799. static void rtl8xxxu_set_basic_rates(struct rtl8xxxu_priv *priv, u32 rate_cfg)
  3800. {
  3801. u32 val32;
  3802. u8 rate_idx = 0;
  3803. rate_cfg &= RESPONSE_RATE_BITMAP_ALL;
  3804. val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
  3805. val32 &= ~RESPONSE_RATE_BITMAP_ALL;
  3806. val32 |= rate_cfg;
  3807. rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
  3808. dev_dbg(&priv->udev->dev, "%s: rates %08x\n", __func__, rate_cfg);
  3809. while (rate_cfg) {
  3810. rate_cfg = (rate_cfg >> 1);
  3811. rate_idx++;
  3812. }
  3813. rtl8xxxu_write8(priv, REG_INIRTS_RATE_SEL, rate_idx);
  3814. }
  3815. static void
  3816. rtl8xxxu_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3817. struct ieee80211_bss_conf *bss_conf, u32 changed)
  3818. {
  3819. struct rtl8xxxu_priv *priv = hw->priv;
  3820. struct device *dev = &priv->udev->dev;
  3821. struct ieee80211_sta *sta;
  3822. u32 val32;
  3823. u8 val8;
  3824. if (changed & BSS_CHANGED_ASSOC) {
  3825. dev_dbg(dev, "Changed ASSOC: %i!\n", bss_conf->assoc);
  3826. rtl8xxxu_set_linktype(priv, vif->type);
  3827. if (bss_conf->assoc) {
  3828. u32 ramask;
  3829. int sgi = 0;
  3830. rcu_read_lock();
  3831. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  3832. if (!sta) {
  3833. dev_info(dev, "%s: ASSOC no sta found\n",
  3834. __func__);
  3835. rcu_read_unlock();
  3836. goto error;
  3837. }
  3838. if (sta->ht_cap.ht_supported)
  3839. dev_info(dev, "%s: HT supported\n", __func__);
  3840. if (sta->vht_cap.vht_supported)
  3841. dev_info(dev, "%s: VHT supported\n", __func__);
  3842. /* TODO: Set bits 28-31 for rate adaptive id */
  3843. ramask = (sta->supp_rates[0] & 0xfff) |
  3844. sta->ht_cap.mcs.rx_mask[0] << 12 |
  3845. sta->ht_cap.mcs.rx_mask[1] << 20;
  3846. if (sta->ht_cap.cap &
  3847. (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
  3848. sgi = 1;
  3849. rcu_read_unlock();
  3850. priv->fops->update_rate_mask(priv, ramask, sgi);
  3851. rtl8xxxu_write8(priv, REG_BCN_MAX_ERR, 0xff);
  3852. rtl8xxxu_stop_tx_beacon(priv);
  3853. /* joinbss sequence */
  3854. rtl8xxxu_write16(priv, REG_BCN_PSR_RPT,
  3855. 0xc000 | bss_conf->aid);
  3856. priv->fops->report_connect(priv, 0, true);
  3857. } else {
  3858. val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
  3859. val8 |= BEACON_DISABLE_TSF_UPDATE;
  3860. rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
  3861. priv->fops->report_connect(priv, 0, false);
  3862. }
  3863. }
  3864. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  3865. dev_dbg(dev, "Changed ERP_PREAMBLE: Use short preamble %i\n",
  3866. bss_conf->use_short_preamble);
  3867. val32 = rtl8xxxu_read32(priv, REG_RESPONSE_RATE_SET);
  3868. if (bss_conf->use_short_preamble)
  3869. val32 |= RSR_ACK_SHORT_PREAMBLE;
  3870. else
  3871. val32 &= ~RSR_ACK_SHORT_PREAMBLE;
  3872. rtl8xxxu_write32(priv, REG_RESPONSE_RATE_SET, val32);
  3873. }
  3874. if (changed & BSS_CHANGED_ERP_SLOT) {
  3875. dev_dbg(dev, "Changed ERP_SLOT: short_slot_time %i\n",
  3876. bss_conf->use_short_slot);
  3877. if (bss_conf->use_short_slot)
  3878. val8 = 9;
  3879. else
  3880. val8 = 20;
  3881. rtl8xxxu_write8(priv, REG_SLOT, val8);
  3882. }
  3883. if (changed & BSS_CHANGED_BSSID) {
  3884. dev_dbg(dev, "Changed BSSID!\n");
  3885. rtl8xxxu_set_bssid(priv, bss_conf->bssid);
  3886. }
  3887. if (changed & BSS_CHANGED_BASIC_RATES) {
  3888. dev_dbg(dev, "Changed BASIC_RATES!\n");
  3889. rtl8xxxu_set_basic_rates(priv, bss_conf->basic_rates);
  3890. }
  3891. error:
  3892. return;
  3893. }
  3894. static u32 rtl8xxxu_80211_to_rtl_queue(u32 queue)
  3895. {
  3896. u32 rtlqueue;
  3897. switch (queue) {
  3898. case IEEE80211_AC_VO:
  3899. rtlqueue = TXDESC_QUEUE_VO;
  3900. break;
  3901. case IEEE80211_AC_VI:
  3902. rtlqueue = TXDESC_QUEUE_VI;
  3903. break;
  3904. case IEEE80211_AC_BE:
  3905. rtlqueue = TXDESC_QUEUE_BE;
  3906. break;
  3907. case IEEE80211_AC_BK:
  3908. rtlqueue = TXDESC_QUEUE_BK;
  3909. break;
  3910. default:
  3911. rtlqueue = TXDESC_QUEUE_BE;
  3912. }
  3913. return rtlqueue;
  3914. }
  3915. static u32 rtl8xxxu_queue_select(struct ieee80211_hw *hw, struct sk_buff *skb)
  3916. {
  3917. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  3918. u32 queue;
  3919. if (ieee80211_is_mgmt(hdr->frame_control))
  3920. queue = TXDESC_QUEUE_MGNT;
  3921. else
  3922. queue = rtl8xxxu_80211_to_rtl_queue(skb_get_queue_mapping(skb));
  3923. return queue;
  3924. }
  3925. /*
  3926. * Despite newer chips 8723b/8812/8821 having a larger TX descriptor
  3927. * format. The descriptor checksum is still only calculated over the
  3928. * initial 32 bytes of the descriptor!
  3929. */
  3930. static void rtl8xxxu_calc_tx_desc_csum(struct rtl8xxxu_txdesc32 *tx_desc)
  3931. {
  3932. __le16 *ptr = (__le16 *)tx_desc;
  3933. u16 csum = 0;
  3934. int i;
  3935. /*
  3936. * Clear csum field before calculation, as the csum field is
  3937. * in the middle of the struct.
  3938. */
  3939. tx_desc->csum = cpu_to_le16(0);
  3940. for (i = 0; i < (sizeof(struct rtl8xxxu_txdesc32) / sizeof(u16)); i++)
  3941. csum = csum ^ le16_to_cpu(ptr[i]);
  3942. tx_desc->csum |= cpu_to_le16(csum);
  3943. }
  3944. static void rtl8xxxu_free_tx_resources(struct rtl8xxxu_priv *priv)
  3945. {
  3946. struct rtl8xxxu_tx_urb *tx_urb, *tmp;
  3947. unsigned long flags;
  3948. spin_lock_irqsave(&priv->tx_urb_lock, flags);
  3949. list_for_each_entry_safe(tx_urb, tmp, &priv->tx_urb_free_list, list) {
  3950. list_del(&tx_urb->list);
  3951. priv->tx_urb_free_count--;
  3952. usb_free_urb(&tx_urb->urb);
  3953. }
  3954. spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
  3955. }
  3956. static struct rtl8xxxu_tx_urb *
  3957. rtl8xxxu_alloc_tx_urb(struct rtl8xxxu_priv *priv)
  3958. {
  3959. struct rtl8xxxu_tx_urb *tx_urb;
  3960. unsigned long flags;
  3961. spin_lock_irqsave(&priv->tx_urb_lock, flags);
  3962. tx_urb = list_first_entry_or_null(&priv->tx_urb_free_list,
  3963. struct rtl8xxxu_tx_urb, list);
  3964. if (tx_urb) {
  3965. list_del(&tx_urb->list);
  3966. priv->tx_urb_free_count--;
  3967. if (priv->tx_urb_free_count < RTL8XXXU_TX_URB_LOW_WATER &&
  3968. !priv->tx_stopped) {
  3969. priv->tx_stopped = true;
  3970. ieee80211_stop_queues(priv->hw);
  3971. }
  3972. }
  3973. spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
  3974. return tx_urb;
  3975. }
  3976. static void rtl8xxxu_free_tx_urb(struct rtl8xxxu_priv *priv,
  3977. struct rtl8xxxu_tx_urb *tx_urb)
  3978. {
  3979. unsigned long flags;
  3980. INIT_LIST_HEAD(&tx_urb->list);
  3981. spin_lock_irqsave(&priv->tx_urb_lock, flags);
  3982. list_add(&tx_urb->list, &priv->tx_urb_free_list);
  3983. priv->tx_urb_free_count++;
  3984. if (priv->tx_urb_free_count > RTL8XXXU_TX_URB_HIGH_WATER &&
  3985. priv->tx_stopped) {
  3986. priv->tx_stopped = false;
  3987. ieee80211_wake_queues(priv->hw);
  3988. }
  3989. spin_unlock_irqrestore(&priv->tx_urb_lock, flags);
  3990. }
  3991. static void rtl8xxxu_tx_complete(struct urb *urb)
  3992. {
  3993. struct sk_buff *skb = (struct sk_buff *)urb->context;
  3994. struct ieee80211_tx_info *tx_info;
  3995. struct ieee80211_hw *hw;
  3996. struct rtl8xxxu_priv *priv;
  3997. struct rtl8xxxu_tx_urb *tx_urb =
  3998. container_of(urb, struct rtl8xxxu_tx_urb, urb);
  3999. tx_info = IEEE80211_SKB_CB(skb);
  4000. hw = tx_info->rate_driver_data[0];
  4001. priv = hw->priv;
  4002. skb_pull(skb, priv->fops->tx_desc_size);
  4003. ieee80211_tx_info_clear_status(tx_info);
  4004. tx_info->status.rates[0].idx = -1;
  4005. tx_info->status.rates[0].count = 0;
  4006. if (!urb->status)
  4007. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  4008. ieee80211_tx_status_irqsafe(hw, skb);
  4009. rtl8xxxu_free_tx_urb(priv, tx_urb);
  4010. }
  4011. static void rtl8xxxu_dump_action(struct device *dev,
  4012. struct ieee80211_hdr *hdr)
  4013. {
  4014. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
  4015. u16 cap, timeout;
  4016. if (!(rtl8xxxu_debug & RTL8XXXU_DEBUG_ACTION))
  4017. return;
  4018. switch (mgmt->u.action.u.addba_resp.action_code) {
  4019. case WLAN_ACTION_ADDBA_RESP:
  4020. cap = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
  4021. timeout = le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
  4022. dev_info(dev, "WLAN_ACTION_ADDBA_RESP: "
  4023. "timeout %i, tid %02x, buf_size %02x, policy %02x, "
  4024. "status %02x\n",
  4025. timeout,
  4026. (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
  4027. (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
  4028. (cap >> 1) & 0x1,
  4029. le16_to_cpu(mgmt->u.action.u.addba_resp.status));
  4030. break;
  4031. case WLAN_ACTION_ADDBA_REQ:
  4032. cap = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
  4033. timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
  4034. dev_info(dev, "WLAN_ACTION_ADDBA_REQ: "
  4035. "timeout %i, tid %02x, buf_size %02x, policy %02x\n",
  4036. timeout,
  4037. (cap & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2,
  4038. (cap & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6,
  4039. (cap >> 1) & 0x1);
  4040. break;
  4041. default:
  4042. dev_info(dev, "action frame %02x\n",
  4043. mgmt->u.action.u.addba_resp.action_code);
  4044. break;
  4045. }
  4046. }
  4047. /*
  4048. * Fill in v1 (gen1) specific TX descriptor bits.
  4049. * This format is used on 8188cu/8192cu/8723au
  4050. */
  4051. void
  4052. rtl8xxxu_fill_txdesc_v1(struct ieee80211_hw *hw, struct ieee80211_hdr *hdr,
  4053. struct ieee80211_tx_info *tx_info,
  4054. struct rtl8xxxu_txdesc32 *tx_desc, bool sgi,
  4055. bool short_preamble, bool ampdu_enable, u32 rts_rate)
  4056. {
  4057. struct ieee80211_rate *tx_rate = ieee80211_get_tx_rate(hw, tx_info);
  4058. struct rtl8xxxu_priv *priv = hw->priv;
  4059. struct device *dev = &priv->udev->dev;
  4060. u32 rate;
  4061. u16 rate_flags = tx_info->control.rates[0].flags;
  4062. u16 seq_number;
  4063. if (rate_flags & IEEE80211_TX_RC_MCS &&
  4064. !ieee80211_is_mgmt(hdr->frame_control))
  4065. rate = tx_info->control.rates[0].idx + DESC_RATE_MCS0;
  4066. else
  4067. rate = tx_rate->hw_value;
  4068. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
  4069. dev_info(dev, "%s: TX rate: %d, pkt size %d\n",
  4070. __func__, rate, cpu_to_le16(tx_desc->pkt_size));
  4071. seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  4072. tx_desc->txdw5 = cpu_to_le32(rate);
  4073. if (ieee80211_is_data(hdr->frame_control))
  4074. tx_desc->txdw5 |= cpu_to_le32(0x0001ff00);
  4075. tx_desc->txdw3 = cpu_to_le32((u32)seq_number << TXDESC32_SEQ_SHIFT);
  4076. if (ampdu_enable)
  4077. tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_ENABLE);
  4078. else
  4079. tx_desc->txdw1 |= cpu_to_le32(TXDESC32_AGG_BREAK);
  4080. if (ieee80211_is_mgmt(hdr->frame_control)) {
  4081. tx_desc->txdw5 = cpu_to_le32(rate);
  4082. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_USE_DRIVER_RATE);
  4083. tx_desc->txdw5 |= cpu_to_le32(6 << TXDESC32_RETRY_LIMIT_SHIFT);
  4084. tx_desc->txdw5 |= cpu_to_le32(TXDESC32_RETRY_LIMIT_ENABLE);
  4085. }
  4086. if (ieee80211_is_data_qos(hdr->frame_control))
  4087. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_QOS);
  4088. if (short_preamble)
  4089. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_SHORT_PREAMBLE);
  4090. if (sgi)
  4091. tx_desc->txdw5 |= cpu_to_le32(TXDESC32_SHORT_GI);
  4092. /*
  4093. * rts_rate is zero if RTS/CTS or CTS to SELF are not enabled
  4094. */
  4095. tx_desc->txdw4 |= cpu_to_le32(rts_rate << TXDESC32_RTS_RATE_SHIFT);
  4096. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  4097. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_RTS_CTS_ENABLE);
  4098. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
  4099. } else if (rate_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  4100. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_CTS_SELF_ENABLE);
  4101. tx_desc->txdw4 |= cpu_to_le32(TXDESC32_HW_RTS_ENABLE);
  4102. }
  4103. }
  4104. /*
  4105. * Fill in v2 (gen2) specific TX descriptor bits.
  4106. * This format is used on 8192eu/8723bu
  4107. */
  4108. void
  4109. rtl8xxxu_fill_txdesc_v2(struct ieee80211_hw *hw, struct ieee80211_hdr *hdr,
  4110. struct ieee80211_tx_info *tx_info,
  4111. struct rtl8xxxu_txdesc32 *tx_desc32, bool sgi,
  4112. bool short_preamble, bool ampdu_enable, u32 rts_rate)
  4113. {
  4114. struct ieee80211_rate *tx_rate = ieee80211_get_tx_rate(hw, tx_info);
  4115. struct rtl8xxxu_priv *priv = hw->priv;
  4116. struct device *dev = &priv->udev->dev;
  4117. struct rtl8xxxu_txdesc40 *tx_desc40;
  4118. u32 rate;
  4119. u16 rate_flags = tx_info->control.rates[0].flags;
  4120. u16 seq_number;
  4121. tx_desc40 = (struct rtl8xxxu_txdesc40 *)tx_desc32;
  4122. if (rate_flags & IEEE80211_TX_RC_MCS &&
  4123. !ieee80211_is_mgmt(hdr->frame_control))
  4124. rate = tx_info->control.rates[0].idx + DESC_RATE_MCS0;
  4125. else
  4126. rate = tx_rate->hw_value;
  4127. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_TX)
  4128. dev_info(dev, "%s: TX rate: %d, pkt size %d\n",
  4129. __func__, rate, cpu_to_le16(tx_desc40->pkt_size));
  4130. seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  4131. tx_desc40->txdw4 = cpu_to_le32(rate);
  4132. if (ieee80211_is_data(hdr->frame_control)) {
  4133. tx_desc40->txdw4 |= cpu_to_le32(0x1f <<
  4134. TXDESC40_DATA_RATE_FB_SHIFT);
  4135. }
  4136. tx_desc40->txdw9 = cpu_to_le32((u32)seq_number << TXDESC40_SEQ_SHIFT);
  4137. if (ampdu_enable)
  4138. tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_ENABLE);
  4139. else
  4140. tx_desc40->txdw2 |= cpu_to_le32(TXDESC40_AGG_BREAK);
  4141. if (ieee80211_is_mgmt(hdr->frame_control)) {
  4142. tx_desc40->txdw4 = cpu_to_le32(rate);
  4143. tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_USE_DRIVER_RATE);
  4144. tx_desc40->txdw4 |=
  4145. cpu_to_le32(6 << TXDESC40_RETRY_LIMIT_SHIFT);
  4146. tx_desc40->txdw4 |= cpu_to_le32(TXDESC40_RETRY_LIMIT_ENABLE);
  4147. }
  4148. if (short_preamble)
  4149. tx_desc40->txdw5 |= cpu_to_le32(TXDESC40_SHORT_PREAMBLE);
  4150. tx_desc40->txdw4 |= cpu_to_le32(rts_rate << TXDESC40_RTS_RATE_SHIFT);
  4151. /*
  4152. * rts_rate is zero if RTS/CTS or CTS to SELF are not enabled
  4153. */
  4154. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  4155. tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_RTS_CTS_ENABLE);
  4156. tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_HW_RTS_ENABLE);
  4157. } else if (rate_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  4158. /*
  4159. * For some reason the vendor driver doesn't set
  4160. * TXDESC40_HW_RTS_ENABLE for CTS to SELF
  4161. */
  4162. tx_desc40->txdw3 |= cpu_to_le32(TXDESC40_CTS_SELF_ENABLE);
  4163. }
  4164. }
  4165. static void rtl8xxxu_tx(struct ieee80211_hw *hw,
  4166. struct ieee80211_tx_control *control,
  4167. struct sk_buff *skb)
  4168. {
  4169. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  4170. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  4171. struct rtl8xxxu_priv *priv = hw->priv;
  4172. struct rtl8xxxu_txdesc32 *tx_desc;
  4173. struct rtl8xxxu_tx_urb *tx_urb;
  4174. struct ieee80211_sta *sta = NULL;
  4175. struct ieee80211_vif *vif = tx_info->control.vif;
  4176. struct device *dev = &priv->udev->dev;
  4177. u32 queue, rts_rate;
  4178. u16 pktlen = skb->len;
  4179. u16 seq_number;
  4180. u16 rate_flag = tx_info->control.rates[0].flags;
  4181. int tx_desc_size = priv->fops->tx_desc_size;
  4182. int ret;
  4183. bool usedesc40, ampdu_enable, sgi = false, short_preamble = false;
  4184. if (skb_headroom(skb) < tx_desc_size) {
  4185. dev_warn(dev,
  4186. "%s: Not enough headroom (%i) for tx descriptor\n",
  4187. __func__, skb_headroom(skb));
  4188. goto error;
  4189. }
  4190. if (unlikely(skb->len > (65535 - tx_desc_size))) {
  4191. dev_warn(dev, "%s: Trying to send over-sized skb (%i)\n",
  4192. __func__, skb->len);
  4193. goto error;
  4194. }
  4195. tx_urb = rtl8xxxu_alloc_tx_urb(priv);
  4196. if (!tx_urb) {
  4197. dev_warn(dev, "%s: Unable to allocate tx urb\n", __func__);
  4198. goto error;
  4199. }
  4200. if (ieee80211_is_action(hdr->frame_control))
  4201. rtl8xxxu_dump_action(dev, hdr);
  4202. usedesc40 = (tx_desc_size == 40);
  4203. tx_info->rate_driver_data[0] = hw;
  4204. if (control && control->sta)
  4205. sta = control->sta;
  4206. tx_desc = skb_push(skb, tx_desc_size);
  4207. memset(tx_desc, 0, tx_desc_size);
  4208. tx_desc->pkt_size = cpu_to_le16(pktlen);
  4209. tx_desc->pkt_offset = tx_desc_size;
  4210. tx_desc->txdw0 =
  4211. TXDESC_OWN | TXDESC_FIRST_SEGMENT | TXDESC_LAST_SEGMENT;
  4212. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
  4213. is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  4214. tx_desc->txdw0 |= TXDESC_BROADMULTICAST;
  4215. queue = rtl8xxxu_queue_select(hw, skb);
  4216. tx_desc->txdw1 = cpu_to_le32(queue << TXDESC_QUEUE_SHIFT);
  4217. if (tx_info->control.hw_key) {
  4218. switch (tx_info->control.hw_key->cipher) {
  4219. case WLAN_CIPHER_SUITE_WEP40:
  4220. case WLAN_CIPHER_SUITE_WEP104:
  4221. case WLAN_CIPHER_SUITE_TKIP:
  4222. tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_RC4);
  4223. break;
  4224. case WLAN_CIPHER_SUITE_CCMP:
  4225. tx_desc->txdw1 |= cpu_to_le32(TXDESC_SEC_AES);
  4226. break;
  4227. default:
  4228. break;
  4229. }
  4230. }
  4231. /* (tx_info->flags & IEEE80211_TX_CTL_AMPDU) && */
  4232. ampdu_enable = false;
  4233. if (ieee80211_is_data_qos(hdr->frame_control) && sta) {
  4234. if (sta->ht_cap.ht_supported) {
  4235. u32 ampdu, val32;
  4236. ampdu = (u32)sta->ht_cap.ampdu_density;
  4237. val32 = ampdu << TXDESC_AMPDU_DENSITY_SHIFT;
  4238. tx_desc->txdw2 |= cpu_to_le32(val32);
  4239. ampdu_enable = true;
  4240. }
  4241. }
  4242. if (rate_flag & IEEE80211_TX_RC_SHORT_GI ||
  4243. (ieee80211_is_data_qos(hdr->frame_control) &&
  4244. sta && sta->ht_cap.cap &
  4245. (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20)))
  4246. sgi = true;
  4247. if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE ||
  4248. (sta && vif && vif->bss_conf.use_short_preamble))
  4249. short_preamble = true;
  4250. if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS)
  4251. rts_rate = ieee80211_get_rts_cts_rate(hw, tx_info)->hw_value;
  4252. else if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT)
  4253. rts_rate = ieee80211_get_rts_cts_rate(hw, tx_info)->hw_value;
  4254. else
  4255. rts_rate = 0;
  4256. seq_number = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  4257. priv->fops->fill_txdesc(hw, hdr, tx_info, tx_desc, sgi, short_preamble,
  4258. ampdu_enable, rts_rate);
  4259. rtl8xxxu_calc_tx_desc_csum(tx_desc);
  4260. usb_fill_bulk_urb(&tx_urb->urb, priv->udev, priv->pipe_out[queue],
  4261. skb->data, skb->len, rtl8xxxu_tx_complete, skb);
  4262. usb_anchor_urb(&tx_urb->urb, &priv->tx_anchor);
  4263. ret = usb_submit_urb(&tx_urb->urb, GFP_ATOMIC);
  4264. if (ret) {
  4265. usb_unanchor_urb(&tx_urb->urb);
  4266. rtl8xxxu_free_tx_urb(priv, tx_urb);
  4267. goto error;
  4268. }
  4269. return;
  4270. error:
  4271. dev_kfree_skb(skb);
  4272. }
  4273. static void rtl8xxxu_rx_parse_phystats(struct rtl8xxxu_priv *priv,
  4274. struct ieee80211_rx_status *rx_status,
  4275. struct rtl8723au_phy_stats *phy_stats,
  4276. u32 rxmcs)
  4277. {
  4278. if (phy_stats->sgi_en)
  4279. rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  4280. if (rxmcs < DESC_RATE_6M) {
  4281. /*
  4282. * Handle PHY stats for CCK rates
  4283. */
  4284. u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
  4285. switch (cck_agc_rpt & 0xc0) {
  4286. case 0xc0:
  4287. rx_status->signal = -46 - (cck_agc_rpt & 0x3e);
  4288. break;
  4289. case 0x80:
  4290. rx_status->signal = -26 - (cck_agc_rpt & 0x3e);
  4291. break;
  4292. case 0x40:
  4293. rx_status->signal = -12 - (cck_agc_rpt & 0x3e);
  4294. break;
  4295. case 0x00:
  4296. rx_status->signal = 16 - (cck_agc_rpt & 0x3e);
  4297. break;
  4298. }
  4299. } else {
  4300. rx_status->signal =
  4301. (phy_stats->cck_sig_qual_ofdm_pwdb_all >> 1) - 110;
  4302. }
  4303. }
  4304. static void rtl8xxxu_free_rx_resources(struct rtl8xxxu_priv *priv)
  4305. {
  4306. struct rtl8xxxu_rx_urb *rx_urb, *tmp;
  4307. unsigned long flags;
  4308. spin_lock_irqsave(&priv->rx_urb_lock, flags);
  4309. list_for_each_entry_safe(rx_urb, tmp,
  4310. &priv->rx_urb_pending_list, list) {
  4311. list_del(&rx_urb->list);
  4312. priv->rx_urb_pending_count--;
  4313. usb_free_urb(&rx_urb->urb);
  4314. }
  4315. spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
  4316. }
  4317. static void rtl8xxxu_queue_rx_urb(struct rtl8xxxu_priv *priv,
  4318. struct rtl8xxxu_rx_urb *rx_urb)
  4319. {
  4320. struct sk_buff *skb;
  4321. unsigned long flags;
  4322. int pending = 0;
  4323. spin_lock_irqsave(&priv->rx_urb_lock, flags);
  4324. if (!priv->shutdown) {
  4325. list_add_tail(&rx_urb->list, &priv->rx_urb_pending_list);
  4326. priv->rx_urb_pending_count++;
  4327. pending = priv->rx_urb_pending_count;
  4328. } else {
  4329. skb = (struct sk_buff *)rx_urb->urb.context;
  4330. dev_kfree_skb(skb);
  4331. usb_free_urb(&rx_urb->urb);
  4332. }
  4333. spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
  4334. if (pending > RTL8XXXU_RX_URB_PENDING_WATER)
  4335. schedule_work(&priv->rx_urb_wq);
  4336. }
  4337. static void rtl8xxxu_rx_urb_work(struct work_struct *work)
  4338. {
  4339. struct rtl8xxxu_priv *priv;
  4340. struct rtl8xxxu_rx_urb *rx_urb, *tmp;
  4341. struct list_head local;
  4342. struct sk_buff *skb;
  4343. unsigned long flags;
  4344. int ret;
  4345. priv = container_of(work, struct rtl8xxxu_priv, rx_urb_wq);
  4346. INIT_LIST_HEAD(&local);
  4347. spin_lock_irqsave(&priv->rx_urb_lock, flags);
  4348. list_splice_init(&priv->rx_urb_pending_list, &local);
  4349. priv->rx_urb_pending_count = 0;
  4350. spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
  4351. list_for_each_entry_safe(rx_urb, tmp, &local, list) {
  4352. list_del_init(&rx_urb->list);
  4353. ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
  4354. /*
  4355. * If out of memory or temporary error, put it back on the
  4356. * queue and try again. Otherwise the device is dead/gone
  4357. * and we should drop it.
  4358. */
  4359. switch (ret) {
  4360. case 0:
  4361. break;
  4362. case -ENOMEM:
  4363. case -EAGAIN:
  4364. rtl8xxxu_queue_rx_urb(priv, rx_urb);
  4365. break;
  4366. default:
  4367. pr_info("failed to requeue urb %i\n", ret);
  4368. skb = (struct sk_buff *)rx_urb->urb.context;
  4369. dev_kfree_skb(skb);
  4370. usb_free_urb(&rx_urb->urb);
  4371. }
  4372. }
  4373. }
  4374. static void rtl8723bu_handle_c2h(struct rtl8xxxu_priv *priv,
  4375. struct sk_buff *skb)
  4376. {
  4377. struct rtl8723bu_c2h *c2h = (struct rtl8723bu_c2h *)skb->data;
  4378. struct device *dev = &priv->udev->dev;
  4379. int len;
  4380. len = skb->len - 2;
  4381. dev_dbg(dev, "C2H ID %02x seq %02x, len %02x source %02x\n",
  4382. c2h->id, c2h->seq, len, c2h->bt_info.response_source);
  4383. switch(c2h->id) {
  4384. case C2H_8723B_BT_INFO:
  4385. if (c2h->bt_info.response_source >
  4386. BT_INFO_SRC_8723B_BT_ACTIVE_SEND)
  4387. dev_dbg(dev, "C2H_BT_INFO WiFi only firmware\n");
  4388. else
  4389. dev_dbg(dev, "C2H_BT_INFO BT/WiFi coexist firmware\n");
  4390. if (c2h->bt_info.bt_has_reset)
  4391. dev_dbg(dev, "BT has been reset\n");
  4392. if (c2h->bt_info.tx_rx_mask)
  4393. dev_dbg(dev, "BT TRx mask\n");
  4394. break;
  4395. case C2H_8723B_BT_MP_INFO:
  4396. dev_dbg(dev, "C2H_MP_INFO ext ID %02x, status %02x\n",
  4397. c2h->bt_mp_info.ext_id, c2h->bt_mp_info.status);
  4398. break;
  4399. case C2H_8723B_RA_REPORT:
  4400. dev_dbg(dev,
  4401. "C2H RA RPT: rate %02x, unk %i, macid %02x, noise %i\n",
  4402. c2h->ra_report.rate, c2h->ra_report.dummy0_0,
  4403. c2h->ra_report.macid, c2h->ra_report.noisy_state);
  4404. break;
  4405. default:
  4406. dev_info(dev, "Unhandled C2H event %02x seq %02x\n",
  4407. c2h->id, c2h->seq);
  4408. print_hex_dump(KERN_INFO, "C2H content: ", DUMP_PREFIX_NONE,
  4409. 16, 1, c2h->raw.payload, len, false);
  4410. break;
  4411. }
  4412. }
  4413. int rtl8xxxu_parse_rxdesc16(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
  4414. {
  4415. struct ieee80211_hw *hw = priv->hw;
  4416. struct ieee80211_rx_status *rx_status;
  4417. struct rtl8xxxu_rxdesc16 *rx_desc;
  4418. struct rtl8723au_phy_stats *phy_stats;
  4419. struct sk_buff *next_skb = NULL;
  4420. __le32 *_rx_desc_le;
  4421. u32 *_rx_desc;
  4422. int drvinfo_sz, desc_shift;
  4423. int i, pkt_cnt, pkt_len, urb_len, pkt_offset;
  4424. urb_len = skb->len;
  4425. pkt_cnt = 0;
  4426. do {
  4427. rx_desc = (struct rtl8xxxu_rxdesc16 *)skb->data;
  4428. _rx_desc_le = (__le32 *)skb->data;
  4429. _rx_desc = (u32 *)skb->data;
  4430. for (i = 0;
  4431. i < (sizeof(struct rtl8xxxu_rxdesc16) / sizeof(u32)); i++)
  4432. _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
  4433. /*
  4434. * Only read pkt_cnt from the header if we're parsing the
  4435. * first packet
  4436. */
  4437. if (!pkt_cnt)
  4438. pkt_cnt = rx_desc->pkt_cnt;
  4439. pkt_len = rx_desc->pktlen;
  4440. drvinfo_sz = rx_desc->drvinfo_sz * 8;
  4441. desc_shift = rx_desc->shift;
  4442. pkt_offset = roundup(pkt_len + drvinfo_sz + desc_shift +
  4443. sizeof(struct rtl8xxxu_rxdesc16), 128);
  4444. /*
  4445. * Only clone the skb if there's enough data at the end to
  4446. * at least cover the rx descriptor
  4447. */
  4448. if (pkt_cnt > 1 &&
  4449. urb_len > (pkt_offset + sizeof(struct rtl8xxxu_rxdesc16)))
  4450. next_skb = skb_clone(skb, GFP_ATOMIC);
  4451. rx_status = IEEE80211_SKB_RXCB(skb);
  4452. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  4453. skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc16));
  4454. phy_stats = (struct rtl8723au_phy_stats *)skb->data;
  4455. skb_pull(skb, drvinfo_sz + desc_shift);
  4456. skb_trim(skb, pkt_len);
  4457. if (rx_desc->phy_stats)
  4458. rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
  4459. rx_desc->rxmcs);
  4460. rx_status->mactime = rx_desc->tsfl;
  4461. rx_status->flag |= RX_FLAG_MACTIME_START;
  4462. if (!rx_desc->swdec)
  4463. rx_status->flag |= RX_FLAG_DECRYPTED;
  4464. if (rx_desc->crc32)
  4465. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  4466. if (rx_desc->bw)
  4467. rx_status->bw = RATE_INFO_BW_40;
  4468. if (rx_desc->rxht) {
  4469. rx_status->encoding = RX_ENC_HT;
  4470. rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
  4471. } else {
  4472. rx_status->rate_idx = rx_desc->rxmcs;
  4473. }
  4474. rx_status->freq = hw->conf.chandef.chan->center_freq;
  4475. rx_status->band = hw->conf.chandef.chan->band;
  4476. ieee80211_rx_irqsafe(hw, skb);
  4477. skb = next_skb;
  4478. if (skb)
  4479. skb_pull(next_skb, pkt_offset);
  4480. pkt_cnt--;
  4481. urb_len -= pkt_offset;
  4482. } while (skb && urb_len > 0 && pkt_cnt > 0);
  4483. return RX_TYPE_DATA_PKT;
  4484. }
  4485. int rtl8xxxu_parse_rxdesc24(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
  4486. {
  4487. struct ieee80211_hw *hw = priv->hw;
  4488. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  4489. struct rtl8xxxu_rxdesc24 *rx_desc =
  4490. (struct rtl8xxxu_rxdesc24 *)skb->data;
  4491. struct rtl8723au_phy_stats *phy_stats;
  4492. __le32 *_rx_desc_le = (__le32 *)skb->data;
  4493. u32 *_rx_desc = (u32 *)skb->data;
  4494. int drvinfo_sz, desc_shift;
  4495. int i;
  4496. for (i = 0; i < (sizeof(struct rtl8xxxu_rxdesc24) / sizeof(u32)); i++)
  4497. _rx_desc[i] = le32_to_cpu(_rx_desc_le[i]);
  4498. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  4499. skb_pull(skb, sizeof(struct rtl8xxxu_rxdesc24));
  4500. phy_stats = (struct rtl8723au_phy_stats *)skb->data;
  4501. drvinfo_sz = rx_desc->drvinfo_sz * 8;
  4502. desc_shift = rx_desc->shift;
  4503. skb_pull(skb, drvinfo_sz + desc_shift);
  4504. if (rx_desc->rpt_sel) {
  4505. struct device *dev = &priv->udev->dev;
  4506. dev_dbg(dev, "%s: C2H packet\n", __func__);
  4507. rtl8723bu_handle_c2h(priv, skb);
  4508. dev_kfree_skb(skb);
  4509. return RX_TYPE_C2H;
  4510. }
  4511. if (rx_desc->phy_stats)
  4512. rtl8xxxu_rx_parse_phystats(priv, rx_status, phy_stats,
  4513. rx_desc->rxmcs);
  4514. rx_status->mactime = rx_desc->tsfl;
  4515. rx_status->flag |= RX_FLAG_MACTIME_START;
  4516. if (!rx_desc->swdec)
  4517. rx_status->flag |= RX_FLAG_DECRYPTED;
  4518. if (rx_desc->crc32)
  4519. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  4520. if (rx_desc->bw)
  4521. rx_status->bw = RATE_INFO_BW_40;
  4522. if (rx_desc->rxmcs >= DESC_RATE_MCS0) {
  4523. rx_status->encoding = RX_ENC_HT;
  4524. rx_status->rate_idx = rx_desc->rxmcs - DESC_RATE_MCS0;
  4525. } else {
  4526. rx_status->rate_idx = rx_desc->rxmcs;
  4527. }
  4528. rx_status->freq = hw->conf.chandef.chan->center_freq;
  4529. rx_status->band = hw->conf.chandef.chan->band;
  4530. ieee80211_rx_irqsafe(hw, skb);
  4531. return RX_TYPE_DATA_PKT;
  4532. }
  4533. static void rtl8xxxu_rx_complete(struct urb *urb)
  4534. {
  4535. struct rtl8xxxu_rx_urb *rx_urb =
  4536. container_of(urb, struct rtl8xxxu_rx_urb, urb);
  4537. struct ieee80211_hw *hw = rx_urb->hw;
  4538. struct rtl8xxxu_priv *priv = hw->priv;
  4539. struct sk_buff *skb = (struct sk_buff *)urb->context;
  4540. struct device *dev = &priv->udev->dev;
  4541. skb_put(skb, urb->actual_length);
  4542. if (urb->status == 0) {
  4543. priv->fops->parse_rx_desc(priv, skb);
  4544. skb = NULL;
  4545. rx_urb->urb.context = NULL;
  4546. rtl8xxxu_queue_rx_urb(priv, rx_urb);
  4547. } else {
  4548. dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
  4549. goto cleanup;
  4550. }
  4551. return;
  4552. cleanup:
  4553. usb_free_urb(urb);
  4554. dev_kfree_skb(skb);
  4555. return;
  4556. }
  4557. static int rtl8xxxu_submit_rx_urb(struct rtl8xxxu_priv *priv,
  4558. struct rtl8xxxu_rx_urb *rx_urb)
  4559. {
  4560. struct rtl8xxxu_fileops *fops = priv->fops;
  4561. struct sk_buff *skb;
  4562. int skb_size;
  4563. int ret, rx_desc_sz;
  4564. rx_desc_sz = fops->rx_desc_size;
  4565. if (priv->rx_buf_aggregation && fops->rx_agg_buf_size) {
  4566. skb_size = fops->rx_agg_buf_size;
  4567. skb_size += (rx_desc_sz + sizeof(struct rtl8723au_phy_stats));
  4568. } else {
  4569. skb_size = IEEE80211_MAX_FRAME_LEN;
  4570. }
  4571. skb = __netdev_alloc_skb(NULL, skb_size, GFP_KERNEL);
  4572. if (!skb)
  4573. return -ENOMEM;
  4574. memset(skb->data, 0, rx_desc_sz);
  4575. usb_fill_bulk_urb(&rx_urb->urb, priv->udev, priv->pipe_in, skb->data,
  4576. skb_size, rtl8xxxu_rx_complete, skb);
  4577. usb_anchor_urb(&rx_urb->urb, &priv->rx_anchor);
  4578. ret = usb_submit_urb(&rx_urb->urb, GFP_ATOMIC);
  4579. if (ret)
  4580. usb_unanchor_urb(&rx_urb->urb);
  4581. return ret;
  4582. }
  4583. static void rtl8xxxu_int_complete(struct urb *urb)
  4584. {
  4585. struct rtl8xxxu_priv *priv = (struct rtl8xxxu_priv *)urb->context;
  4586. struct device *dev = &priv->udev->dev;
  4587. int ret;
  4588. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_INTERRUPT)
  4589. dev_dbg(dev, "%s: status %i\n", __func__, urb->status);
  4590. if (urb->status == 0) {
  4591. usb_anchor_urb(urb, &priv->int_anchor);
  4592. ret = usb_submit_urb(urb, GFP_ATOMIC);
  4593. if (ret)
  4594. usb_unanchor_urb(urb);
  4595. } else {
  4596. dev_dbg(dev, "%s: Error %i\n", __func__, urb->status);
  4597. }
  4598. }
  4599. static int rtl8xxxu_submit_int_urb(struct ieee80211_hw *hw)
  4600. {
  4601. struct rtl8xxxu_priv *priv = hw->priv;
  4602. struct urb *urb;
  4603. u32 val32;
  4604. int ret;
  4605. urb = usb_alloc_urb(0, GFP_KERNEL);
  4606. if (!urb)
  4607. return -ENOMEM;
  4608. usb_fill_int_urb(urb, priv->udev, priv->pipe_interrupt,
  4609. priv->int_buf, USB_INTR_CONTENT_LENGTH,
  4610. rtl8xxxu_int_complete, priv, 1);
  4611. usb_anchor_urb(urb, &priv->int_anchor);
  4612. ret = usb_submit_urb(urb, GFP_KERNEL);
  4613. if (ret) {
  4614. usb_unanchor_urb(urb);
  4615. usb_free_urb(urb);
  4616. goto error;
  4617. }
  4618. val32 = rtl8xxxu_read32(priv, REG_USB_HIMR);
  4619. val32 |= USB_HIMR_CPWM;
  4620. rtl8xxxu_write32(priv, REG_USB_HIMR, val32);
  4621. error:
  4622. return ret;
  4623. }
  4624. static int rtl8xxxu_add_interface(struct ieee80211_hw *hw,
  4625. struct ieee80211_vif *vif)
  4626. {
  4627. struct rtl8xxxu_priv *priv = hw->priv;
  4628. int ret;
  4629. u8 val8;
  4630. switch (vif->type) {
  4631. case NL80211_IFTYPE_STATION:
  4632. rtl8xxxu_stop_tx_beacon(priv);
  4633. val8 = rtl8xxxu_read8(priv, REG_BEACON_CTRL);
  4634. val8 |= BEACON_ATIM | BEACON_FUNCTION_ENABLE |
  4635. BEACON_DISABLE_TSF_UPDATE;
  4636. rtl8xxxu_write8(priv, REG_BEACON_CTRL, val8);
  4637. ret = 0;
  4638. break;
  4639. default:
  4640. ret = -EOPNOTSUPP;
  4641. }
  4642. rtl8xxxu_set_linktype(priv, vif->type);
  4643. return ret;
  4644. }
  4645. static void rtl8xxxu_remove_interface(struct ieee80211_hw *hw,
  4646. struct ieee80211_vif *vif)
  4647. {
  4648. struct rtl8xxxu_priv *priv = hw->priv;
  4649. dev_dbg(&priv->udev->dev, "%s\n", __func__);
  4650. }
  4651. static int rtl8xxxu_config(struct ieee80211_hw *hw, u32 changed)
  4652. {
  4653. struct rtl8xxxu_priv *priv = hw->priv;
  4654. struct device *dev = &priv->udev->dev;
  4655. u16 val16;
  4656. int ret = 0, channel;
  4657. bool ht40;
  4658. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_CHANNEL)
  4659. dev_info(dev,
  4660. "%s: channel: %i (changed %08x chandef.width %02x)\n",
  4661. __func__, hw->conf.chandef.chan->hw_value,
  4662. changed, hw->conf.chandef.width);
  4663. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  4664. val16 = ((hw->conf.long_frame_max_tx_count <<
  4665. RETRY_LIMIT_LONG_SHIFT) & RETRY_LIMIT_LONG_MASK) |
  4666. ((hw->conf.short_frame_max_tx_count <<
  4667. RETRY_LIMIT_SHORT_SHIFT) & RETRY_LIMIT_SHORT_MASK);
  4668. rtl8xxxu_write16(priv, REG_RETRY_LIMIT, val16);
  4669. }
  4670. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  4671. switch (hw->conf.chandef.width) {
  4672. case NL80211_CHAN_WIDTH_20_NOHT:
  4673. case NL80211_CHAN_WIDTH_20:
  4674. ht40 = false;
  4675. break;
  4676. case NL80211_CHAN_WIDTH_40:
  4677. ht40 = true;
  4678. break;
  4679. default:
  4680. ret = -ENOTSUPP;
  4681. goto exit;
  4682. }
  4683. channel = hw->conf.chandef.chan->hw_value;
  4684. priv->fops->set_tx_power(priv, channel, ht40);
  4685. priv->fops->config_channel(hw);
  4686. }
  4687. exit:
  4688. return ret;
  4689. }
  4690. static int rtl8xxxu_conf_tx(struct ieee80211_hw *hw,
  4691. struct ieee80211_vif *vif, u16 queue,
  4692. const struct ieee80211_tx_queue_params *param)
  4693. {
  4694. struct rtl8xxxu_priv *priv = hw->priv;
  4695. struct device *dev = &priv->udev->dev;
  4696. u32 val32;
  4697. u8 aifs, acm_ctrl, acm_bit;
  4698. aifs = param->aifs;
  4699. val32 = aifs |
  4700. fls(param->cw_min) << EDCA_PARAM_ECW_MIN_SHIFT |
  4701. fls(param->cw_max) << EDCA_PARAM_ECW_MAX_SHIFT |
  4702. (u32)param->txop << EDCA_PARAM_TXOP_SHIFT;
  4703. acm_ctrl = rtl8xxxu_read8(priv, REG_ACM_HW_CTRL);
  4704. dev_dbg(dev,
  4705. "%s: IEEE80211 queue %02x val %08x, acm %i, acm_ctrl %02x\n",
  4706. __func__, queue, val32, param->acm, acm_ctrl);
  4707. switch (queue) {
  4708. case IEEE80211_AC_VO:
  4709. acm_bit = ACM_HW_CTRL_VO;
  4710. rtl8xxxu_write32(priv, REG_EDCA_VO_PARAM, val32);
  4711. break;
  4712. case IEEE80211_AC_VI:
  4713. acm_bit = ACM_HW_CTRL_VI;
  4714. rtl8xxxu_write32(priv, REG_EDCA_VI_PARAM, val32);
  4715. break;
  4716. case IEEE80211_AC_BE:
  4717. acm_bit = ACM_HW_CTRL_BE;
  4718. rtl8xxxu_write32(priv, REG_EDCA_BE_PARAM, val32);
  4719. break;
  4720. case IEEE80211_AC_BK:
  4721. acm_bit = ACM_HW_CTRL_BK;
  4722. rtl8xxxu_write32(priv, REG_EDCA_BK_PARAM, val32);
  4723. break;
  4724. default:
  4725. acm_bit = 0;
  4726. break;
  4727. }
  4728. if (param->acm)
  4729. acm_ctrl |= acm_bit;
  4730. else
  4731. acm_ctrl &= ~acm_bit;
  4732. rtl8xxxu_write8(priv, REG_ACM_HW_CTRL, acm_ctrl);
  4733. return 0;
  4734. }
  4735. static void rtl8xxxu_configure_filter(struct ieee80211_hw *hw,
  4736. unsigned int changed_flags,
  4737. unsigned int *total_flags, u64 multicast)
  4738. {
  4739. struct rtl8xxxu_priv *priv = hw->priv;
  4740. u32 rcr = rtl8xxxu_read32(priv, REG_RCR);
  4741. dev_dbg(&priv->udev->dev, "%s: changed_flags %08x, total_flags %08x\n",
  4742. __func__, changed_flags, *total_flags);
  4743. /*
  4744. * FIF_ALLMULTI ignored as all multicast frames are accepted (REG_MAR)
  4745. */
  4746. if (*total_flags & FIF_FCSFAIL)
  4747. rcr |= RCR_ACCEPT_CRC32;
  4748. else
  4749. rcr &= ~RCR_ACCEPT_CRC32;
  4750. /*
  4751. * FIF_PLCPFAIL not supported?
  4752. */
  4753. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  4754. rcr &= ~RCR_CHECK_BSSID_BEACON;
  4755. else
  4756. rcr |= RCR_CHECK_BSSID_BEACON;
  4757. if (*total_flags & FIF_CONTROL)
  4758. rcr |= RCR_ACCEPT_CTRL_FRAME;
  4759. else
  4760. rcr &= ~RCR_ACCEPT_CTRL_FRAME;
  4761. if (*total_flags & FIF_OTHER_BSS) {
  4762. rcr |= RCR_ACCEPT_AP;
  4763. rcr &= ~RCR_CHECK_BSSID_MATCH;
  4764. } else {
  4765. rcr &= ~RCR_ACCEPT_AP;
  4766. rcr |= RCR_CHECK_BSSID_MATCH;
  4767. }
  4768. if (*total_flags & FIF_PSPOLL)
  4769. rcr |= RCR_ACCEPT_PM;
  4770. else
  4771. rcr &= ~RCR_ACCEPT_PM;
  4772. /*
  4773. * FIF_PROBE_REQ ignored as probe requests always seem to be accepted
  4774. */
  4775. rtl8xxxu_write32(priv, REG_RCR, rcr);
  4776. *total_flags &= (FIF_ALLMULTI | FIF_FCSFAIL | FIF_BCN_PRBRESP_PROMISC |
  4777. FIF_CONTROL | FIF_OTHER_BSS | FIF_PSPOLL |
  4778. FIF_PROBE_REQ);
  4779. }
  4780. static int rtl8xxxu_set_rts_threshold(struct ieee80211_hw *hw, u32 rts)
  4781. {
  4782. if (rts > 2347)
  4783. return -EINVAL;
  4784. return 0;
  4785. }
  4786. static int rtl8xxxu_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  4787. struct ieee80211_vif *vif,
  4788. struct ieee80211_sta *sta,
  4789. struct ieee80211_key_conf *key)
  4790. {
  4791. struct rtl8xxxu_priv *priv = hw->priv;
  4792. struct device *dev = &priv->udev->dev;
  4793. u8 mac_addr[ETH_ALEN];
  4794. u8 val8;
  4795. u16 val16;
  4796. u32 val32;
  4797. int retval = -EOPNOTSUPP;
  4798. dev_dbg(dev, "%s: cmd %02x, cipher %08x, index %i\n",
  4799. __func__, cmd, key->cipher, key->keyidx);
  4800. if (vif->type != NL80211_IFTYPE_STATION)
  4801. return -EOPNOTSUPP;
  4802. if (key->keyidx > 3)
  4803. return -EOPNOTSUPP;
  4804. switch (key->cipher) {
  4805. case WLAN_CIPHER_SUITE_WEP40:
  4806. case WLAN_CIPHER_SUITE_WEP104:
  4807. break;
  4808. case WLAN_CIPHER_SUITE_CCMP:
  4809. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  4810. break;
  4811. case WLAN_CIPHER_SUITE_TKIP:
  4812. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  4813. break;
  4814. default:
  4815. return -EOPNOTSUPP;
  4816. }
  4817. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  4818. dev_dbg(dev, "%s: pairwise key\n", __func__);
  4819. ether_addr_copy(mac_addr, sta->addr);
  4820. } else {
  4821. dev_dbg(dev, "%s: group key\n", __func__);
  4822. eth_broadcast_addr(mac_addr);
  4823. }
  4824. val16 = rtl8xxxu_read16(priv, REG_CR);
  4825. val16 |= CR_SECURITY_ENABLE;
  4826. rtl8xxxu_write16(priv, REG_CR, val16);
  4827. val8 = SEC_CFG_TX_SEC_ENABLE | SEC_CFG_TXBC_USE_DEFKEY |
  4828. SEC_CFG_RX_SEC_ENABLE | SEC_CFG_RXBC_USE_DEFKEY;
  4829. val8 |= SEC_CFG_TX_USE_DEFKEY | SEC_CFG_RX_USE_DEFKEY;
  4830. rtl8xxxu_write8(priv, REG_SECURITY_CFG, val8);
  4831. switch (cmd) {
  4832. case SET_KEY:
  4833. key->hw_key_idx = key->keyidx;
  4834. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  4835. rtl8xxxu_cam_write(priv, key, mac_addr);
  4836. retval = 0;
  4837. break;
  4838. case DISABLE_KEY:
  4839. rtl8xxxu_write32(priv, REG_CAM_WRITE, 0x00000000);
  4840. val32 = CAM_CMD_POLLING | CAM_CMD_WRITE |
  4841. key->keyidx << CAM_CMD_KEY_SHIFT;
  4842. rtl8xxxu_write32(priv, REG_CAM_CMD, val32);
  4843. retval = 0;
  4844. break;
  4845. default:
  4846. dev_warn(dev, "%s: Unsupported command %02x\n", __func__, cmd);
  4847. }
  4848. return retval;
  4849. }
  4850. static int
  4851. rtl8xxxu_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4852. struct ieee80211_ampdu_params *params)
  4853. {
  4854. struct rtl8xxxu_priv *priv = hw->priv;
  4855. struct device *dev = &priv->udev->dev;
  4856. u8 ampdu_factor, ampdu_density;
  4857. struct ieee80211_sta *sta = params->sta;
  4858. enum ieee80211_ampdu_mlme_action action = params->action;
  4859. switch (action) {
  4860. case IEEE80211_AMPDU_TX_START:
  4861. dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_START\n", __func__);
  4862. ampdu_factor = sta->ht_cap.ampdu_factor;
  4863. ampdu_density = sta->ht_cap.ampdu_density;
  4864. rtl8xxxu_set_ampdu_factor(priv, ampdu_factor);
  4865. rtl8xxxu_set_ampdu_min_space(priv, ampdu_density);
  4866. dev_dbg(dev,
  4867. "Changed HT: ampdu_factor %02x, ampdu_density %02x\n",
  4868. ampdu_factor, ampdu_density);
  4869. break;
  4870. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4871. dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH\n", __func__);
  4872. rtl8xxxu_set_ampdu_factor(priv, 0);
  4873. rtl8xxxu_set_ampdu_min_space(priv, 0);
  4874. break;
  4875. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4876. dev_dbg(dev, "%s: IEEE80211_AMPDU_TX_STOP_FLUSH_CONT\n",
  4877. __func__);
  4878. rtl8xxxu_set_ampdu_factor(priv, 0);
  4879. rtl8xxxu_set_ampdu_min_space(priv, 0);
  4880. break;
  4881. case IEEE80211_AMPDU_RX_START:
  4882. dev_dbg(dev, "%s: IEEE80211_AMPDU_RX_START\n", __func__);
  4883. break;
  4884. case IEEE80211_AMPDU_RX_STOP:
  4885. dev_dbg(dev, "%s: IEEE80211_AMPDU_RX_STOP\n", __func__);
  4886. break;
  4887. default:
  4888. break;
  4889. }
  4890. return 0;
  4891. }
  4892. static int rtl8xxxu_start(struct ieee80211_hw *hw)
  4893. {
  4894. struct rtl8xxxu_priv *priv = hw->priv;
  4895. struct rtl8xxxu_rx_urb *rx_urb;
  4896. struct rtl8xxxu_tx_urb *tx_urb;
  4897. unsigned long flags;
  4898. int ret, i;
  4899. ret = 0;
  4900. init_usb_anchor(&priv->rx_anchor);
  4901. init_usb_anchor(&priv->tx_anchor);
  4902. init_usb_anchor(&priv->int_anchor);
  4903. priv->fops->enable_rf(priv);
  4904. if (priv->usb_interrupts) {
  4905. ret = rtl8xxxu_submit_int_urb(hw);
  4906. if (ret)
  4907. goto exit;
  4908. }
  4909. for (i = 0; i < RTL8XXXU_TX_URBS; i++) {
  4910. tx_urb = kmalloc(sizeof(struct rtl8xxxu_tx_urb), GFP_KERNEL);
  4911. if (!tx_urb) {
  4912. if (!i)
  4913. ret = -ENOMEM;
  4914. goto error_out;
  4915. }
  4916. usb_init_urb(&tx_urb->urb);
  4917. INIT_LIST_HEAD(&tx_urb->list);
  4918. tx_urb->hw = hw;
  4919. list_add(&tx_urb->list, &priv->tx_urb_free_list);
  4920. priv->tx_urb_free_count++;
  4921. }
  4922. priv->tx_stopped = false;
  4923. spin_lock_irqsave(&priv->rx_urb_lock, flags);
  4924. priv->shutdown = false;
  4925. spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
  4926. for (i = 0; i < RTL8XXXU_RX_URBS; i++) {
  4927. rx_urb = kmalloc(sizeof(struct rtl8xxxu_rx_urb), GFP_KERNEL);
  4928. if (!rx_urb) {
  4929. if (!i)
  4930. ret = -ENOMEM;
  4931. goto error_out;
  4932. }
  4933. usb_init_urb(&rx_urb->urb);
  4934. INIT_LIST_HEAD(&rx_urb->list);
  4935. rx_urb->hw = hw;
  4936. ret = rtl8xxxu_submit_rx_urb(priv, rx_urb);
  4937. }
  4938. exit:
  4939. /*
  4940. * Accept all data and mgmt frames
  4941. */
  4942. rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0xffff);
  4943. rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0xffff);
  4944. rtl8xxxu_write32(priv, REG_OFDM0_XA_AGC_CORE1, 0x6954341e);
  4945. return ret;
  4946. error_out:
  4947. rtl8xxxu_free_tx_resources(priv);
  4948. /*
  4949. * Disable all data and mgmt frames
  4950. */
  4951. rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
  4952. rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
  4953. return ret;
  4954. }
  4955. static void rtl8xxxu_stop(struct ieee80211_hw *hw)
  4956. {
  4957. struct rtl8xxxu_priv *priv = hw->priv;
  4958. unsigned long flags;
  4959. rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
  4960. rtl8xxxu_write16(priv, REG_RXFLTMAP0, 0x0000);
  4961. rtl8xxxu_write16(priv, REG_RXFLTMAP2, 0x0000);
  4962. spin_lock_irqsave(&priv->rx_urb_lock, flags);
  4963. priv->shutdown = true;
  4964. spin_unlock_irqrestore(&priv->rx_urb_lock, flags);
  4965. usb_kill_anchored_urbs(&priv->rx_anchor);
  4966. usb_kill_anchored_urbs(&priv->tx_anchor);
  4967. if (priv->usb_interrupts)
  4968. usb_kill_anchored_urbs(&priv->int_anchor);
  4969. rtl8xxxu_write8(priv, REG_TXPAUSE, 0xff);
  4970. priv->fops->disable_rf(priv);
  4971. /*
  4972. * Disable interrupts
  4973. */
  4974. if (priv->usb_interrupts)
  4975. rtl8xxxu_write32(priv, REG_USB_HIMR, 0);
  4976. rtl8xxxu_free_rx_resources(priv);
  4977. rtl8xxxu_free_tx_resources(priv);
  4978. }
  4979. static const struct ieee80211_ops rtl8xxxu_ops = {
  4980. .tx = rtl8xxxu_tx,
  4981. .add_interface = rtl8xxxu_add_interface,
  4982. .remove_interface = rtl8xxxu_remove_interface,
  4983. .config = rtl8xxxu_config,
  4984. .conf_tx = rtl8xxxu_conf_tx,
  4985. .bss_info_changed = rtl8xxxu_bss_info_changed,
  4986. .configure_filter = rtl8xxxu_configure_filter,
  4987. .set_rts_threshold = rtl8xxxu_set_rts_threshold,
  4988. .start = rtl8xxxu_start,
  4989. .stop = rtl8xxxu_stop,
  4990. .sw_scan_start = rtl8xxxu_sw_scan_start,
  4991. .sw_scan_complete = rtl8xxxu_sw_scan_complete,
  4992. .set_key = rtl8xxxu_set_key,
  4993. .ampdu_action = rtl8xxxu_ampdu_action,
  4994. };
  4995. static int rtl8xxxu_parse_usb(struct rtl8xxxu_priv *priv,
  4996. struct usb_interface *interface)
  4997. {
  4998. struct usb_interface_descriptor *interface_desc;
  4999. struct usb_host_interface *host_interface;
  5000. struct usb_endpoint_descriptor *endpoint;
  5001. struct device *dev = &priv->udev->dev;
  5002. int i, j = 0, endpoints;
  5003. u8 dir, xtype, num;
  5004. int ret = 0;
  5005. host_interface = interface->cur_altsetting;
  5006. interface_desc = &host_interface->desc;
  5007. endpoints = interface_desc->bNumEndpoints;
  5008. for (i = 0; i < endpoints; i++) {
  5009. endpoint = &host_interface->endpoint[i].desc;
  5010. dir = endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
  5011. num = usb_endpoint_num(endpoint);
  5012. xtype = usb_endpoint_type(endpoint);
  5013. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
  5014. dev_dbg(dev,
  5015. "%s: endpoint: dir %02x, # %02x, type %02x\n",
  5016. __func__, dir, num, xtype);
  5017. if (usb_endpoint_dir_in(endpoint) &&
  5018. usb_endpoint_xfer_bulk(endpoint)) {
  5019. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
  5020. dev_dbg(dev, "%s: in endpoint num %i\n",
  5021. __func__, num);
  5022. if (priv->pipe_in) {
  5023. dev_warn(dev,
  5024. "%s: Too many IN pipes\n", __func__);
  5025. ret = -EINVAL;
  5026. goto exit;
  5027. }
  5028. priv->pipe_in = usb_rcvbulkpipe(priv->udev, num);
  5029. }
  5030. if (usb_endpoint_dir_in(endpoint) &&
  5031. usb_endpoint_xfer_int(endpoint)) {
  5032. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
  5033. dev_dbg(dev, "%s: interrupt endpoint num %i\n",
  5034. __func__, num);
  5035. if (priv->pipe_interrupt) {
  5036. dev_warn(dev, "%s: Too many INTERRUPT pipes\n",
  5037. __func__);
  5038. ret = -EINVAL;
  5039. goto exit;
  5040. }
  5041. priv->pipe_interrupt = usb_rcvintpipe(priv->udev, num);
  5042. }
  5043. if (usb_endpoint_dir_out(endpoint) &&
  5044. usb_endpoint_xfer_bulk(endpoint)) {
  5045. if (rtl8xxxu_debug & RTL8XXXU_DEBUG_USB)
  5046. dev_dbg(dev, "%s: out endpoint num %i\n",
  5047. __func__, num);
  5048. if (j >= RTL8XXXU_OUT_ENDPOINTS) {
  5049. dev_warn(dev,
  5050. "%s: Too many OUT pipes\n", __func__);
  5051. ret = -EINVAL;
  5052. goto exit;
  5053. }
  5054. priv->out_ep[j++] = num;
  5055. }
  5056. }
  5057. exit:
  5058. priv->nr_out_eps = j;
  5059. return ret;
  5060. }
  5061. static int rtl8xxxu_probe(struct usb_interface *interface,
  5062. const struct usb_device_id *id)
  5063. {
  5064. struct rtl8xxxu_priv *priv;
  5065. struct ieee80211_hw *hw;
  5066. struct usb_device *udev;
  5067. struct ieee80211_supported_band *sband;
  5068. int ret;
  5069. int untested = 1;
  5070. udev = usb_get_dev(interface_to_usbdev(interface));
  5071. switch (id->idVendor) {
  5072. case USB_VENDOR_ID_REALTEK:
  5073. switch(id->idProduct) {
  5074. case 0x1724:
  5075. case 0x8176:
  5076. case 0x8178:
  5077. case 0x817f:
  5078. case 0x818b:
  5079. untested = 0;
  5080. break;
  5081. }
  5082. break;
  5083. case 0x7392:
  5084. if (id->idProduct == 0x7811)
  5085. untested = 0;
  5086. break;
  5087. case 0x050d:
  5088. if (id->idProduct == 0x1004)
  5089. untested = 0;
  5090. break;
  5091. case 0x20f4:
  5092. if (id->idProduct == 0x648b)
  5093. untested = 0;
  5094. break;
  5095. case 0x2001:
  5096. if (id->idProduct == 0x3308)
  5097. untested = 0;
  5098. break;
  5099. case 0x2357:
  5100. if (id->idProduct == 0x0109)
  5101. untested = 0;
  5102. break;
  5103. default:
  5104. break;
  5105. }
  5106. if (untested) {
  5107. rtl8xxxu_debug |= RTL8XXXU_DEBUG_EFUSE;
  5108. dev_info(&udev->dev,
  5109. "This Realtek USB WiFi dongle (0x%04x:0x%04x) is untested!\n",
  5110. id->idVendor, id->idProduct);
  5111. dev_info(&udev->dev,
  5112. "Please report results to Jes.Sorensen@gmail.com\n");
  5113. }
  5114. hw = ieee80211_alloc_hw(sizeof(struct rtl8xxxu_priv), &rtl8xxxu_ops);
  5115. if (!hw) {
  5116. ret = -ENOMEM;
  5117. priv = NULL;
  5118. goto exit;
  5119. }
  5120. priv = hw->priv;
  5121. priv->hw = hw;
  5122. priv->udev = udev;
  5123. priv->fops = (struct rtl8xxxu_fileops *)id->driver_info;
  5124. mutex_init(&priv->usb_buf_mutex);
  5125. mutex_init(&priv->h2c_mutex);
  5126. INIT_LIST_HEAD(&priv->tx_urb_free_list);
  5127. spin_lock_init(&priv->tx_urb_lock);
  5128. INIT_LIST_HEAD(&priv->rx_urb_pending_list);
  5129. spin_lock_init(&priv->rx_urb_lock);
  5130. INIT_WORK(&priv->rx_urb_wq, rtl8xxxu_rx_urb_work);
  5131. usb_set_intfdata(interface, hw);
  5132. ret = rtl8xxxu_parse_usb(priv, interface);
  5133. if (ret)
  5134. goto exit;
  5135. ret = rtl8xxxu_identify_chip(priv);
  5136. if (ret) {
  5137. dev_err(&udev->dev, "Fatal - failed to identify chip\n");
  5138. goto exit;
  5139. }
  5140. ret = rtl8xxxu_read_efuse(priv);
  5141. if (ret) {
  5142. dev_err(&udev->dev, "Fatal - failed to read EFuse\n");
  5143. goto exit;
  5144. }
  5145. ret = priv->fops->parse_efuse(priv);
  5146. if (ret) {
  5147. dev_err(&udev->dev, "Fatal - failed to parse EFuse\n");
  5148. goto exit;
  5149. }
  5150. rtl8xxxu_print_chipinfo(priv);
  5151. ret = priv->fops->load_firmware(priv);
  5152. if (ret) {
  5153. dev_err(&udev->dev, "Fatal - failed to load firmware\n");
  5154. goto exit;
  5155. }
  5156. ret = rtl8xxxu_init_device(hw);
  5157. if (ret)
  5158. goto exit;
  5159. hw->wiphy->max_scan_ssids = 1;
  5160. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  5161. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  5162. hw->queues = 4;
  5163. sband = &rtl8xxxu_supported_band;
  5164. sband->ht_cap.ht_supported = true;
  5165. sband->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  5166. sband->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
  5167. sband->ht_cap.cap = IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40;
  5168. memset(&sband->ht_cap.mcs, 0, sizeof(sband->ht_cap.mcs));
  5169. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  5170. sband->ht_cap.mcs.rx_mask[4] = 0x01;
  5171. if (priv->rf_paths > 1) {
  5172. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  5173. sband->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  5174. }
  5175. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  5176. /*
  5177. * Some APs will negotiate HT20_40 in a noisy environment leading
  5178. * to miserable performance. Rather than defaulting to this, only
  5179. * enable it if explicitly requested at module load time.
  5180. */
  5181. if (rtl8xxxu_ht40_2g) {
  5182. dev_info(&udev->dev, "Enabling HT_20_40 on the 2.4GHz band\n");
  5183. sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  5184. }
  5185. hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
  5186. hw->wiphy->rts_threshold = 2347;
  5187. SET_IEEE80211_DEV(priv->hw, &interface->dev);
  5188. SET_IEEE80211_PERM_ADDR(hw, priv->mac_addr);
  5189. hw->extra_tx_headroom = priv->fops->tx_desc_size;
  5190. ieee80211_hw_set(hw, SIGNAL_DBM);
  5191. /*
  5192. * The firmware handles rate control
  5193. */
  5194. ieee80211_hw_set(hw, HAS_RATE_CONTROL);
  5195. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  5196. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  5197. ret = ieee80211_register_hw(priv->hw);
  5198. if (ret) {
  5199. dev_err(&udev->dev, "%s: Failed to register: %i\n",
  5200. __func__, ret);
  5201. goto exit;
  5202. }
  5203. return 0;
  5204. exit:
  5205. usb_set_intfdata(interface, NULL);
  5206. if (priv) {
  5207. kfree(priv->fw_data);
  5208. mutex_destroy(&priv->usb_buf_mutex);
  5209. mutex_destroy(&priv->h2c_mutex);
  5210. }
  5211. usb_put_dev(udev);
  5212. ieee80211_free_hw(hw);
  5213. return ret;
  5214. }
  5215. static void rtl8xxxu_disconnect(struct usb_interface *interface)
  5216. {
  5217. struct rtl8xxxu_priv *priv;
  5218. struct ieee80211_hw *hw;
  5219. hw = usb_get_intfdata(interface);
  5220. priv = hw->priv;
  5221. ieee80211_unregister_hw(hw);
  5222. priv->fops->power_off(priv);
  5223. usb_set_intfdata(interface, NULL);
  5224. dev_info(&priv->udev->dev, "disconnecting\n");
  5225. kfree(priv->fw_data);
  5226. mutex_destroy(&priv->usb_buf_mutex);
  5227. mutex_destroy(&priv->h2c_mutex);
  5228. if (priv->udev->state != USB_STATE_NOTATTACHED) {
  5229. dev_info(&priv->udev->dev,
  5230. "Device still attached, trying to reset\n");
  5231. usb_reset_device(priv->udev);
  5232. }
  5233. usb_put_dev(priv->udev);
  5234. ieee80211_free_hw(hw);
  5235. }
  5236. static const struct usb_device_id dev_table[] = {
  5237. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8724, 0xff, 0xff, 0xff),
  5238. .driver_info = (unsigned long)&rtl8723au_fops},
  5239. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1724, 0xff, 0xff, 0xff),
  5240. .driver_info = (unsigned long)&rtl8723au_fops},
  5241. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x0724, 0xff, 0xff, 0xff),
  5242. .driver_info = (unsigned long)&rtl8723au_fops},
  5243. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818b, 0xff, 0xff, 0xff),
  5244. .driver_info = (unsigned long)&rtl8192eu_fops},
  5245. /* TP-Link TL-WN822N v4 */
  5246. {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0108, 0xff, 0xff, 0xff),
  5247. .driver_info = (unsigned long)&rtl8192eu_fops},
  5248. /* D-Link DWA-131 rev E1, tested by David Patiño */
  5249. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3319, 0xff, 0xff, 0xff),
  5250. .driver_info = (unsigned long)&rtl8192eu_fops},
  5251. /* Tested by Myckel Habets */
  5252. {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0109, 0xff, 0xff, 0xff),
  5253. .driver_info = (unsigned long)&rtl8192eu_fops},
  5254. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0xb720, 0xff, 0xff, 0xff),
  5255. .driver_info = (unsigned long)&rtl8723bu_fops},
  5256. #ifdef CONFIG_RTL8XXXU_UNTESTED
  5257. /* Still supported by rtlwifi */
  5258. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8176, 0xff, 0xff, 0xff),
  5259. .driver_info = (unsigned long)&rtl8192cu_fops},
  5260. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8178, 0xff, 0xff, 0xff),
  5261. .driver_info = (unsigned long)&rtl8192cu_fops},
  5262. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817f, 0xff, 0xff, 0xff),
  5263. .driver_info = (unsigned long)&rtl8192cu_fops},
  5264. /* Tested by Larry Finger */
  5265. {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7811, 0xff, 0xff, 0xff),
  5266. .driver_info = (unsigned long)&rtl8192cu_fops},
  5267. /* Tested by Andrea Merello */
  5268. {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1004, 0xff, 0xff, 0xff),
  5269. .driver_info = (unsigned long)&rtl8192cu_fops},
  5270. /* Tested by Jocelyn Mayer */
  5271. {USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x648b, 0xff, 0xff, 0xff),
  5272. .driver_info = (unsigned long)&rtl8192cu_fops},
  5273. /* Tested by Stefano Bravi */
  5274. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3308, 0xff, 0xff, 0xff),
  5275. .driver_info = (unsigned long)&rtl8192cu_fops},
  5276. /* Currently untested 8188 series devices */
  5277. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8191, 0xff, 0xff, 0xff),
  5278. .driver_info = (unsigned long)&rtl8192cu_fops},
  5279. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8170, 0xff, 0xff, 0xff),
  5280. .driver_info = (unsigned long)&rtl8192cu_fops},
  5281. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x8177, 0xff, 0xff, 0xff),
  5282. .driver_info = (unsigned long)&rtl8192cu_fops},
  5283. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817a, 0xff, 0xff, 0xff),
  5284. .driver_info = (unsigned long)&rtl8192cu_fops},
  5285. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817b, 0xff, 0xff, 0xff),
  5286. .driver_info = (unsigned long)&rtl8192cu_fops},
  5287. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817d, 0xff, 0xff, 0xff),
  5288. .driver_info = (unsigned long)&rtl8192cu_fops},
  5289. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x817e, 0xff, 0xff, 0xff),
  5290. .driver_info = (unsigned long)&rtl8192cu_fops},
  5291. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818a, 0xff, 0xff, 0xff),
  5292. .driver_info = (unsigned long)&rtl8192cu_fops},
  5293. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x317f, 0xff, 0xff, 0xff),
  5294. .driver_info = (unsigned long)&rtl8192cu_fops},
  5295. {USB_DEVICE_AND_INTERFACE_INFO(0x1058, 0x0631, 0xff, 0xff, 0xff),
  5296. .driver_info = (unsigned long)&rtl8192cu_fops},
  5297. {USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x094c, 0xff, 0xff, 0xff),
  5298. .driver_info = (unsigned long)&rtl8192cu_fops},
  5299. {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x1102, 0xff, 0xff, 0xff),
  5300. .driver_info = (unsigned long)&rtl8192cu_fops},
  5301. {USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe033, 0xff, 0xff, 0xff),
  5302. .driver_info = (unsigned long)&rtl8192cu_fops},
  5303. {USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8189, 0xff, 0xff, 0xff),
  5304. .driver_info = (unsigned long)&rtl8192cu_fops},
  5305. {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9041, 0xff, 0xff, 0xff),
  5306. .driver_info = (unsigned long)&rtl8192cu_fops},
  5307. {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ba, 0xff, 0xff, 0xff),
  5308. .driver_info = (unsigned long)&rtl8192cu_fops},
  5309. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x1e1e, 0xff, 0xff, 0xff),
  5310. .driver_info = (unsigned long)&rtl8192cu_fops},
  5311. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x5088, 0xff, 0xff, 0xff),
  5312. .driver_info = (unsigned long)&rtl8192cu_fops},
  5313. {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0052, 0xff, 0xff, 0xff),
  5314. .driver_info = (unsigned long)&rtl8192cu_fops},
  5315. {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x005c, 0xff, 0xff, 0xff),
  5316. .driver_info = (unsigned long)&rtl8192cu_fops},
  5317. {USB_DEVICE_AND_INTERFACE_INFO(0x0eb0, 0x9071, 0xff, 0xff, 0xff),
  5318. .driver_info = (unsigned long)&rtl8192cu_fops},
  5319. {USB_DEVICE_AND_INTERFACE_INFO(0x103c, 0x1629, 0xff, 0xff, 0xff),
  5320. .driver_info = (unsigned long)&rtl8192cu_fops},
  5321. {USB_DEVICE_AND_INTERFACE_INFO(0x13d3, 0x3357, 0xff, 0xff, 0xff),
  5322. .driver_info = (unsigned long)&rtl8192cu_fops},
  5323. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330b, 0xff, 0xff, 0xff),
  5324. .driver_info = (unsigned long)&rtl8192cu_fops},
  5325. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x4902, 0xff, 0xff, 0xff),
  5326. .driver_info = (unsigned long)&rtl8192cu_fops},
  5327. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2a, 0xff, 0xff, 0xff),
  5328. .driver_info = (unsigned long)&rtl8192cu_fops},
  5329. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2e, 0xff, 0xff, 0xff),
  5330. .driver_info = (unsigned long)&rtl8192cu_fops},
  5331. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xed17, 0xff, 0xff, 0xff),
  5332. .driver_info = (unsigned long)&rtl8192cu_fops},
  5333. {USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0090, 0xff, 0xff, 0xff),
  5334. .driver_info = (unsigned long)&rtl8192cu_fops},
  5335. {USB_DEVICE_AND_INTERFACE_INFO(0x4856, 0x0091, 0xff, 0xff, 0xff),
  5336. .driver_info = (unsigned long)&rtl8192cu_fops},
  5337. {USB_DEVICE_AND_INTERFACE_INFO(0xcdab, 0x8010, 0xff, 0xff, 0xff),
  5338. .driver_info = (unsigned long)&rtl8192cu_fops},
  5339. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff7, 0xff, 0xff, 0xff),
  5340. .driver_info = (unsigned long)&rtl8192cu_fops},
  5341. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff9, 0xff, 0xff, 0xff),
  5342. .driver_info = (unsigned long)&rtl8192cu_fops},
  5343. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffa, 0xff, 0xff, 0xff),
  5344. .driver_info = (unsigned long)&rtl8192cu_fops},
  5345. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaff8, 0xff, 0xff, 0xff),
  5346. .driver_info = (unsigned long)&rtl8192cu_fops},
  5347. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffb, 0xff, 0xff, 0xff),
  5348. .driver_info = (unsigned long)&rtl8192cu_fops},
  5349. {USB_DEVICE_AND_INTERFACE_INFO(0x04f2, 0xaffc, 0xff, 0xff, 0xff),
  5350. .driver_info = (unsigned long)&rtl8192cu_fops},
  5351. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0x1201, 0xff, 0xff, 0xff),
  5352. .driver_info = (unsigned long)&rtl8192cu_fops},
  5353. /* Currently untested 8192 series devices */
  5354. {USB_DEVICE_AND_INTERFACE_INFO(0x04bb, 0x0950, 0xff, 0xff, 0xff),
  5355. .driver_info = (unsigned long)&rtl8192cu_fops},
  5356. {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2102, 0xff, 0xff, 0xff),
  5357. .driver_info = (unsigned long)&rtl8192cu_fops},
  5358. {USB_DEVICE_AND_INTERFACE_INFO(0x050d, 0x2103, 0xff, 0xff, 0xff),
  5359. .driver_info = (unsigned long)&rtl8192cu_fops},
  5360. {USB_DEVICE_AND_INTERFACE_INFO(0x0586, 0x341f, 0xff, 0xff, 0xff),
  5361. .driver_info = (unsigned long)&rtl8192cu_fops},
  5362. {USB_DEVICE_AND_INTERFACE_INFO(0x06f8, 0xe035, 0xff, 0xff, 0xff),
  5363. .driver_info = (unsigned long)&rtl8192cu_fops},
  5364. {USB_DEVICE_AND_INTERFACE_INFO(0x0b05, 0x17ab, 0xff, 0xff, 0xff),
  5365. .driver_info = (unsigned long)&rtl8192cu_fops},
  5366. {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0061, 0xff, 0xff, 0xff),
  5367. .driver_info = (unsigned long)&rtl8192cu_fops},
  5368. {USB_DEVICE_AND_INTERFACE_INFO(0x0df6, 0x0070, 0xff, 0xff, 0xff),
  5369. .driver_info = (unsigned long)&rtl8192cu_fops},
  5370. {USB_DEVICE_AND_INTERFACE_INFO(0x0789, 0x016d, 0xff, 0xff, 0xff),
  5371. .driver_info = (unsigned long)&rtl8192cu_fops},
  5372. {USB_DEVICE_AND_INTERFACE_INFO(0x07aa, 0x0056, 0xff, 0xff, 0xff),
  5373. .driver_info = (unsigned long)&rtl8192cu_fops},
  5374. {USB_DEVICE_AND_INTERFACE_INFO(0x07b8, 0x8178, 0xff, 0xff, 0xff),
  5375. .driver_info = (unsigned long)&rtl8192cu_fops},
  5376. {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0x9021, 0xff, 0xff, 0xff),
  5377. .driver_info = (unsigned long)&rtl8192cu_fops},
  5378. {USB_DEVICE_AND_INTERFACE_INFO(0x0846, 0xf001, 0xff, 0xff, 0xff),
  5379. .driver_info = (unsigned long)&rtl8192cu_fops},
  5380. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x2e2e, 0xff, 0xff, 0xff),
  5381. .driver_info = (unsigned long)&rtl8192cu_fops},
  5382. {USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0019, 0xff, 0xff, 0xff),
  5383. .driver_info = (unsigned long)&rtl8192cu_fops},
  5384. {USB_DEVICE_AND_INTERFACE_INFO(0x0e66, 0x0020, 0xff, 0xff, 0xff),
  5385. .driver_info = (unsigned long)&rtl8192cu_fops},
  5386. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3307, 0xff, 0xff, 0xff),
  5387. .driver_info = (unsigned long)&rtl8192cu_fops},
  5388. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x3309, 0xff, 0xff, 0xff),
  5389. .driver_info = (unsigned long)&rtl8192cu_fops},
  5390. {USB_DEVICE_AND_INTERFACE_INFO(0x2001, 0x330a, 0xff, 0xff, 0xff),
  5391. .driver_info = (unsigned long)&rtl8192cu_fops},
  5392. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab2b, 0xff, 0xff, 0xff),
  5393. .driver_info = (unsigned long)&rtl8192cu_fops},
  5394. {USB_DEVICE_AND_INTERFACE_INFO(0x20f4, 0x624d, 0xff, 0xff, 0xff),
  5395. .driver_info = (unsigned long)&rtl8192cu_fops},
  5396. {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0100, 0xff, 0xff, 0xff),
  5397. .driver_info = (unsigned long)&rtl8192cu_fops},
  5398. {USB_DEVICE_AND_INTERFACE_INFO(0x4855, 0x0091, 0xff, 0xff, 0xff),
  5399. .driver_info = (unsigned long)&rtl8192cu_fops},
  5400. {USB_DEVICE_AND_INTERFACE_INFO(0x7392, 0x7822, 0xff, 0xff, 0xff),
  5401. .driver_info = (unsigned long)&rtl8192cu_fops},
  5402. /* found in rtl8192eu vendor driver */
  5403. {USB_DEVICE_AND_INTERFACE_INFO(0x2357, 0x0107, 0xff, 0xff, 0xff),
  5404. .driver_info = (unsigned long)&rtl8192eu_fops},
  5405. {USB_DEVICE_AND_INTERFACE_INFO(0x2019, 0xab33, 0xff, 0xff, 0xff),
  5406. .driver_info = (unsigned long)&rtl8192eu_fops},
  5407. {USB_DEVICE_AND_INTERFACE_INFO(USB_VENDOR_ID_REALTEK, 0x818c, 0xff, 0xff, 0xff),
  5408. .driver_info = (unsigned long)&rtl8192eu_fops},
  5409. #endif
  5410. { }
  5411. };
  5412. static struct usb_driver rtl8xxxu_driver = {
  5413. .name = DRIVER_NAME,
  5414. .probe = rtl8xxxu_probe,
  5415. .disconnect = rtl8xxxu_disconnect,
  5416. .id_table = dev_table,
  5417. .no_dynamic_id = 1,
  5418. .disable_hub_initiated_lpm = 1,
  5419. };
  5420. static int __init rtl8xxxu_module_init(void)
  5421. {
  5422. int res;
  5423. res = usb_register(&rtl8xxxu_driver);
  5424. if (res < 0)
  5425. pr_err(DRIVER_NAME ": usb_register() failed (%i)\n", res);
  5426. return res;
  5427. }
  5428. static void __exit rtl8xxxu_module_exit(void)
  5429. {
  5430. usb_deregister(&rtl8xxxu_driver);
  5431. }
  5432. MODULE_DEVICE_TABLE(usb, dev_table);
  5433. module_init(rtl8xxxu_module_init);
  5434. module_exit(rtl8xxxu_module_exit);