main.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848
  1. /*
  2. Broadcom B43 wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
  4. Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
  5. Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  6. Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
  7. Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Some parts of the code in this file are derived from the ipw2200
  9. driver Copyright(c) 2003 - 2004 Intel Corporation.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  21. Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/firmware.h>
  29. #include <linux/wireless.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/io.h>
  33. #include <linux/dma-mapping.h>
  34. #include <asm/unaligned.h>
  35. #include "b43.h"
  36. #include "main.h"
  37. #include "debugfs.h"
  38. #include "phy_common.h"
  39. #include "phy_g.h"
  40. #include "phy_n.h"
  41. #include "dma.h"
  42. #include "pio.h"
  43. #include "sysfs.h"
  44. #include "xmit.h"
  45. #include "lo.h"
  46. #include "pcmcia.h"
  47. MODULE_DESCRIPTION("Broadcom B43 wireless driver");
  48. MODULE_AUTHOR("Martin Langer");
  49. MODULE_AUTHOR("Stefano Brivio");
  50. MODULE_AUTHOR("Michael Buesch");
  51. MODULE_LICENSE("GPL");
  52. MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
  53. static int modparam_bad_frames_preempt;
  54. module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
  55. MODULE_PARM_DESC(bad_frames_preempt,
  56. "enable(1) / disable(0) Bad Frames Preemption");
  57. static char modparam_fwpostfix[16];
  58. module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
  59. MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
  60. static int modparam_hwpctl;
  61. module_param_named(hwpctl, modparam_hwpctl, int, 0444);
  62. MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
  63. static int modparam_nohwcrypt;
  64. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  65. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  66. int b43_modparam_qos = 1;
  67. module_param_named(qos, b43_modparam_qos, int, 0444);
  68. MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
  69. static int modparam_btcoex = 1;
  70. module_param_named(btcoex, modparam_btcoex, int, 0444);
  71. MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
  72. static const struct ssb_device_id b43_ssb_tbl[] = {
  73. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
  74. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
  75. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
  76. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
  77. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
  78. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
  79. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
  80. SSB_DEVTABLE_END
  81. };
  82. MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
  83. /* Channel and ratetables are shared for all devices.
  84. * They can't be const, because ieee80211 puts some precalculated
  85. * data in there. This data is the same for all devices, so we don't
  86. * get concurrency issues */
  87. #define RATETAB_ENT(_rateid, _flags) \
  88. { \
  89. .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
  90. .hw_value = (_rateid), \
  91. .flags = (_flags), \
  92. }
  93. /*
  94. * NOTE: When changing this, sync with xmit.c's
  95. * b43_plcp_get_bitrate_idx_* functions!
  96. */
  97. static struct ieee80211_rate __b43_ratetable[] = {
  98. RATETAB_ENT(B43_CCK_RATE_1MB, 0),
  99. RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
  100. RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
  101. RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
  102. RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
  103. RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
  104. RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
  105. RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
  106. RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
  107. RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
  108. RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
  109. RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
  110. };
  111. #define b43_a_ratetable (__b43_ratetable + 4)
  112. #define b43_a_ratetable_size 8
  113. #define b43_b_ratetable (__b43_ratetable + 0)
  114. #define b43_b_ratetable_size 4
  115. #define b43_g_ratetable (__b43_ratetable + 0)
  116. #define b43_g_ratetable_size 12
  117. #define CHAN4G(_channel, _freq, _flags) { \
  118. .band = IEEE80211_BAND_2GHZ, \
  119. .center_freq = (_freq), \
  120. .hw_value = (_channel), \
  121. .flags = (_flags), \
  122. .max_antenna_gain = 0, \
  123. .max_power = 30, \
  124. }
  125. static struct ieee80211_channel b43_2ghz_chantable[] = {
  126. CHAN4G(1, 2412, 0),
  127. CHAN4G(2, 2417, 0),
  128. CHAN4G(3, 2422, 0),
  129. CHAN4G(4, 2427, 0),
  130. CHAN4G(5, 2432, 0),
  131. CHAN4G(6, 2437, 0),
  132. CHAN4G(7, 2442, 0),
  133. CHAN4G(8, 2447, 0),
  134. CHAN4G(9, 2452, 0),
  135. CHAN4G(10, 2457, 0),
  136. CHAN4G(11, 2462, 0),
  137. CHAN4G(12, 2467, 0),
  138. CHAN4G(13, 2472, 0),
  139. CHAN4G(14, 2484, 0),
  140. };
  141. #undef CHAN4G
  142. #define CHAN5G(_channel, _flags) { \
  143. .band = IEEE80211_BAND_5GHZ, \
  144. .center_freq = 5000 + (5 * (_channel)), \
  145. .hw_value = (_channel), \
  146. .flags = (_flags), \
  147. .max_antenna_gain = 0, \
  148. .max_power = 30, \
  149. }
  150. static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
  151. CHAN5G(32, 0), CHAN5G(34, 0),
  152. CHAN5G(36, 0), CHAN5G(38, 0),
  153. CHAN5G(40, 0), CHAN5G(42, 0),
  154. CHAN5G(44, 0), CHAN5G(46, 0),
  155. CHAN5G(48, 0), CHAN5G(50, 0),
  156. CHAN5G(52, 0), CHAN5G(54, 0),
  157. CHAN5G(56, 0), CHAN5G(58, 0),
  158. CHAN5G(60, 0), CHAN5G(62, 0),
  159. CHAN5G(64, 0), CHAN5G(66, 0),
  160. CHAN5G(68, 0), CHAN5G(70, 0),
  161. CHAN5G(72, 0), CHAN5G(74, 0),
  162. CHAN5G(76, 0), CHAN5G(78, 0),
  163. CHAN5G(80, 0), CHAN5G(82, 0),
  164. CHAN5G(84, 0), CHAN5G(86, 0),
  165. CHAN5G(88, 0), CHAN5G(90, 0),
  166. CHAN5G(92, 0), CHAN5G(94, 0),
  167. CHAN5G(96, 0), CHAN5G(98, 0),
  168. CHAN5G(100, 0), CHAN5G(102, 0),
  169. CHAN5G(104, 0), CHAN5G(106, 0),
  170. CHAN5G(108, 0), CHAN5G(110, 0),
  171. CHAN5G(112, 0), CHAN5G(114, 0),
  172. CHAN5G(116, 0), CHAN5G(118, 0),
  173. CHAN5G(120, 0), CHAN5G(122, 0),
  174. CHAN5G(124, 0), CHAN5G(126, 0),
  175. CHAN5G(128, 0), CHAN5G(130, 0),
  176. CHAN5G(132, 0), CHAN5G(134, 0),
  177. CHAN5G(136, 0), CHAN5G(138, 0),
  178. CHAN5G(140, 0), CHAN5G(142, 0),
  179. CHAN5G(144, 0), CHAN5G(145, 0),
  180. CHAN5G(146, 0), CHAN5G(147, 0),
  181. CHAN5G(148, 0), CHAN5G(149, 0),
  182. CHAN5G(150, 0), CHAN5G(151, 0),
  183. CHAN5G(152, 0), CHAN5G(153, 0),
  184. CHAN5G(154, 0), CHAN5G(155, 0),
  185. CHAN5G(156, 0), CHAN5G(157, 0),
  186. CHAN5G(158, 0), CHAN5G(159, 0),
  187. CHAN5G(160, 0), CHAN5G(161, 0),
  188. CHAN5G(162, 0), CHAN5G(163, 0),
  189. CHAN5G(164, 0), CHAN5G(165, 0),
  190. CHAN5G(166, 0), CHAN5G(168, 0),
  191. CHAN5G(170, 0), CHAN5G(172, 0),
  192. CHAN5G(174, 0), CHAN5G(176, 0),
  193. CHAN5G(178, 0), CHAN5G(180, 0),
  194. CHAN5G(182, 0), CHAN5G(184, 0),
  195. CHAN5G(186, 0), CHAN5G(188, 0),
  196. CHAN5G(190, 0), CHAN5G(192, 0),
  197. CHAN5G(194, 0), CHAN5G(196, 0),
  198. CHAN5G(198, 0), CHAN5G(200, 0),
  199. CHAN5G(202, 0), CHAN5G(204, 0),
  200. CHAN5G(206, 0), CHAN5G(208, 0),
  201. CHAN5G(210, 0), CHAN5G(212, 0),
  202. CHAN5G(214, 0), CHAN5G(216, 0),
  203. CHAN5G(218, 0), CHAN5G(220, 0),
  204. CHAN5G(222, 0), CHAN5G(224, 0),
  205. CHAN5G(226, 0), CHAN5G(228, 0),
  206. };
  207. static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
  208. CHAN5G(34, 0), CHAN5G(36, 0),
  209. CHAN5G(38, 0), CHAN5G(40, 0),
  210. CHAN5G(42, 0), CHAN5G(44, 0),
  211. CHAN5G(46, 0), CHAN5G(48, 0),
  212. CHAN5G(52, 0), CHAN5G(56, 0),
  213. CHAN5G(60, 0), CHAN5G(64, 0),
  214. CHAN5G(100, 0), CHAN5G(104, 0),
  215. CHAN5G(108, 0), CHAN5G(112, 0),
  216. CHAN5G(116, 0), CHAN5G(120, 0),
  217. CHAN5G(124, 0), CHAN5G(128, 0),
  218. CHAN5G(132, 0), CHAN5G(136, 0),
  219. CHAN5G(140, 0), CHAN5G(149, 0),
  220. CHAN5G(153, 0), CHAN5G(157, 0),
  221. CHAN5G(161, 0), CHAN5G(165, 0),
  222. CHAN5G(184, 0), CHAN5G(188, 0),
  223. CHAN5G(192, 0), CHAN5G(196, 0),
  224. CHAN5G(200, 0), CHAN5G(204, 0),
  225. CHAN5G(208, 0), CHAN5G(212, 0),
  226. CHAN5G(216, 0),
  227. };
  228. #undef CHAN5G
  229. static struct ieee80211_supported_band b43_band_5GHz_nphy = {
  230. .band = IEEE80211_BAND_5GHZ,
  231. .channels = b43_5ghz_nphy_chantable,
  232. .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
  233. .bitrates = b43_a_ratetable,
  234. .n_bitrates = b43_a_ratetable_size,
  235. };
  236. static struct ieee80211_supported_band b43_band_5GHz_aphy = {
  237. .band = IEEE80211_BAND_5GHZ,
  238. .channels = b43_5ghz_aphy_chantable,
  239. .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
  240. .bitrates = b43_a_ratetable,
  241. .n_bitrates = b43_a_ratetable_size,
  242. };
  243. static struct ieee80211_supported_band b43_band_2GHz = {
  244. .band = IEEE80211_BAND_2GHZ,
  245. .channels = b43_2ghz_chantable,
  246. .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
  247. .bitrates = b43_g_ratetable,
  248. .n_bitrates = b43_g_ratetable_size,
  249. };
  250. static void b43_wireless_core_exit(struct b43_wldev *dev);
  251. static int b43_wireless_core_init(struct b43_wldev *dev);
  252. static void b43_wireless_core_stop(struct b43_wldev *dev);
  253. static int b43_wireless_core_start(struct b43_wldev *dev);
  254. static int b43_ratelimit(struct b43_wl *wl)
  255. {
  256. if (!wl || !wl->current_dev)
  257. return 1;
  258. if (b43_status(wl->current_dev) < B43_STAT_STARTED)
  259. return 1;
  260. /* We are up and running.
  261. * Ratelimit the messages to avoid DoS over the net. */
  262. return net_ratelimit();
  263. }
  264. void b43info(struct b43_wl *wl, const char *fmt, ...)
  265. {
  266. va_list args;
  267. if (!b43_ratelimit(wl))
  268. return;
  269. va_start(args, fmt);
  270. printk(KERN_INFO "b43-%s: ",
  271. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  272. vprintk(fmt, args);
  273. va_end(args);
  274. }
  275. void b43err(struct b43_wl *wl, const char *fmt, ...)
  276. {
  277. va_list args;
  278. if (!b43_ratelimit(wl))
  279. return;
  280. va_start(args, fmt);
  281. printk(KERN_ERR "b43-%s ERROR: ",
  282. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  283. vprintk(fmt, args);
  284. va_end(args);
  285. }
  286. void b43warn(struct b43_wl *wl, const char *fmt, ...)
  287. {
  288. va_list args;
  289. if (!b43_ratelimit(wl))
  290. return;
  291. va_start(args, fmt);
  292. printk(KERN_WARNING "b43-%s warning: ",
  293. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  294. vprintk(fmt, args);
  295. va_end(args);
  296. }
  297. #if B43_DEBUG
  298. void b43dbg(struct b43_wl *wl, const char *fmt, ...)
  299. {
  300. va_list args;
  301. va_start(args, fmt);
  302. printk(KERN_DEBUG "b43-%s debug: ",
  303. (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
  304. vprintk(fmt, args);
  305. va_end(args);
  306. }
  307. #endif /* DEBUG */
  308. static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
  309. {
  310. u32 macctl;
  311. B43_WARN_ON(offset % 4 != 0);
  312. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  313. if (macctl & B43_MACCTL_BE)
  314. val = swab32(val);
  315. b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
  316. mmiowb();
  317. b43_write32(dev, B43_MMIO_RAM_DATA, val);
  318. }
  319. static inline void b43_shm_control_word(struct b43_wldev *dev,
  320. u16 routing, u16 offset)
  321. {
  322. u32 control;
  323. /* "offset" is the WORD offset. */
  324. control = routing;
  325. control <<= 16;
  326. control |= offset;
  327. b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
  328. }
  329. u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  330. {
  331. u32 ret;
  332. if (routing == B43_SHM_SHARED) {
  333. B43_WARN_ON(offset & 0x0001);
  334. if (offset & 0x0003) {
  335. /* Unaligned access */
  336. b43_shm_control_word(dev, routing, offset >> 2);
  337. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  338. ret <<= 16;
  339. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  340. ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
  341. goto out;
  342. }
  343. offset >>= 2;
  344. }
  345. b43_shm_control_word(dev, routing, offset);
  346. ret = b43_read32(dev, B43_MMIO_SHM_DATA);
  347. out:
  348. return ret;
  349. }
  350. u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
  351. {
  352. struct b43_wl *wl = dev->wl;
  353. unsigned long flags;
  354. u32 ret;
  355. spin_lock_irqsave(&wl->shm_lock, flags);
  356. ret = __b43_shm_read32(dev, routing, offset);
  357. spin_unlock_irqrestore(&wl->shm_lock, flags);
  358. return ret;
  359. }
  360. u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  361. {
  362. u16 ret;
  363. if (routing == B43_SHM_SHARED) {
  364. B43_WARN_ON(offset & 0x0001);
  365. if (offset & 0x0003) {
  366. /* Unaligned access */
  367. b43_shm_control_word(dev, routing, offset >> 2);
  368. ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
  369. goto out;
  370. }
  371. offset >>= 2;
  372. }
  373. b43_shm_control_word(dev, routing, offset);
  374. ret = b43_read16(dev, B43_MMIO_SHM_DATA);
  375. out:
  376. return ret;
  377. }
  378. u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
  379. {
  380. struct b43_wl *wl = dev->wl;
  381. unsigned long flags;
  382. u16 ret;
  383. spin_lock_irqsave(&wl->shm_lock, flags);
  384. ret = __b43_shm_read16(dev, routing, offset);
  385. spin_unlock_irqrestore(&wl->shm_lock, flags);
  386. return ret;
  387. }
  388. void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  389. {
  390. if (routing == B43_SHM_SHARED) {
  391. B43_WARN_ON(offset & 0x0001);
  392. if (offset & 0x0003) {
  393. /* Unaligned access */
  394. b43_shm_control_word(dev, routing, offset >> 2);
  395. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
  396. (value >> 16) & 0xffff);
  397. b43_shm_control_word(dev, routing, (offset >> 2) + 1);
  398. b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
  399. return;
  400. }
  401. offset >>= 2;
  402. }
  403. b43_shm_control_word(dev, routing, offset);
  404. b43_write32(dev, B43_MMIO_SHM_DATA, value);
  405. }
  406. void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
  407. {
  408. struct b43_wl *wl = dev->wl;
  409. unsigned long flags;
  410. spin_lock_irqsave(&wl->shm_lock, flags);
  411. __b43_shm_write32(dev, routing, offset, value);
  412. spin_unlock_irqrestore(&wl->shm_lock, flags);
  413. }
  414. void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  415. {
  416. if (routing == B43_SHM_SHARED) {
  417. B43_WARN_ON(offset & 0x0001);
  418. if (offset & 0x0003) {
  419. /* Unaligned access */
  420. b43_shm_control_word(dev, routing, offset >> 2);
  421. b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
  422. return;
  423. }
  424. offset >>= 2;
  425. }
  426. b43_shm_control_word(dev, routing, offset);
  427. b43_write16(dev, B43_MMIO_SHM_DATA, value);
  428. }
  429. void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
  430. {
  431. struct b43_wl *wl = dev->wl;
  432. unsigned long flags;
  433. spin_lock_irqsave(&wl->shm_lock, flags);
  434. __b43_shm_write16(dev, routing, offset, value);
  435. spin_unlock_irqrestore(&wl->shm_lock, flags);
  436. }
  437. /* Read HostFlags */
  438. u64 b43_hf_read(struct b43_wldev * dev)
  439. {
  440. u64 ret;
  441. ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
  442. ret <<= 16;
  443. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI);
  444. ret <<= 16;
  445. ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
  446. return ret;
  447. }
  448. /* Write HostFlags */
  449. void b43_hf_write(struct b43_wldev *dev, u64 value)
  450. {
  451. u16 lo, mi, hi;
  452. lo = (value & 0x00000000FFFFULL);
  453. mi = (value & 0x0000FFFF0000ULL) >> 16;
  454. hi = (value & 0xFFFF00000000ULL) >> 32;
  455. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO, lo);
  456. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI, mi);
  457. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
  458. }
  459. void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
  460. {
  461. /* We need to be careful. As we read the TSF from multiple
  462. * registers, we should take care of register overflows.
  463. * In theory, the whole tsf read process should be atomic.
  464. * We try to be atomic here, by restaring the read process,
  465. * if any of the high registers changed (overflew).
  466. */
  467. if (dev->dev->id.revision >= 3) {
  468. u32 low, high, high2;
  469. do {
  470. high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  471. low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
  472. high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
  473. } while (unlikely(high != high2));
  474. *tsf = high;
  475. *tsf <<= 32;
  476. *tsf |= low;
  477. } else {
  478. u64 tmp;
  479. u16 v0, v1, v2, v3;
  480. u16 test1, test2, test3;
  481. do {
  482. v3 = b43_read16(dev, B43_MMIO_TSF_3);
  483. v2 = b43_read16(dev, B43_MMIO_TSF_2);
  484. v1 = b43_read16(dev, B43_MMIO_TSF_1);
  485. v0 = b43_read16(dev, B43_MMIO_TSF_0);
  486. test3 = b43_read16(dev, B43_MMIO_TSF_3);
  487. test2 = b43_read16(dev, B43_MMIO_TSF_2);
  488. test1 = b43_read16(dev, B43_MMIO_TSF_1);
  489. } while (v3 != test3 || v2 != test2 || v1 != test1);
  490. *tsf = v3;
  491. *tsf <<= 48;
  492. tmp = v2;
  493. tmp <<= 32;
  494. *tsf |= tmp;
  495. tmp = v1;
  496. tmp <<= 16;
  497. *tsf |= tmp;
  498. *tsf |= v0;
  499. }
  500. }
  501. static void b43_time_lock(struct b43_wldev *dev)
  502. {
  503. u32 macctl;
  504. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  505. macctl |= B43_MACCTL_TBTTHOLD;
  506. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  507. /* Commit the write */
  508. b43_read32(dev, B43_MMIO_MACCTL);
  509. }
  510. static void b43_time_unlock(struct b43_wldev *dev)
  511. {
  512. u32 macctl;
  513. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  514. macctl &= ~B43_MACCTL_TBTTHOLD;
  515. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  516. /* Commit the write */
  517. b43_read32(dev, B43_MMIO_MACCTL);
  518. }
  519. static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
  520. {
  521. /* Be careful with the in-progress timer.
  522. * First zero out the low register, so we have a full
  523. * register-overflow duration to complete the operation.
  524. */
  525. if (dev->dev->id.revision >= 3) {
  526. u32 lo = (tsf & 0x00000000FFFFFFFFULL);
  527. u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
  528. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
  529. mmiowb();
  530. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
  531. mmiowb();
  532. b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
  533. } else {
  534. u16 v0 = (tsf & 0x000000000000FFFFULL);
  535. u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
  536. u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
  537. u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
  538. b43_write16(dev, B43_MMIO_TSF_0, 0);
  539. mmiowb();
  540. b43_write16(dev, B43_MMIO_TSF_3, v3);
  541. mmiowb();
  542. b43_write16(dev, B43_MMIO_TSF_2, v2);
  543. mmiowb();
  544. b43_write16(dev, B43_MMIO_TSF_1, v1);
  545. mmiowb();
  546. b43_write16(dev, B43_MMIO_TSF_0, v0);
  547. }
  548. }
  549. void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
  550. {
  551. b43_time_lock(dev);
  552. b43_tsf_write_locked(dev, tsf);
  553. b43_time_unlock(dev);
  554. }
  555. static
  556. void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
  557. {
  558. static const u8 zero_addr[ETH_ALEN] = { 0 };
  559. u16 data;
  560. if (!mac)
  561. mac = zero_addr;
  562. offset |= 0x0020;
  563. b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
  564. data = mac[0];
  565. data |= mac[1] << 8;
  566. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  567. data = mac[2];
  568. data |= mac[3] << 8;
  569. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  570. data = mac[4];
  571. data |= mac[5] << 8;
  572. b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
  573. }
  574. static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
  575. {
  576. const u8 *mac;
  577. const u8 *bssid;
  578. u8 mac_bssid[ETH_ALEN * 2];
  579. int i;
  580. u32 tmp;
  581. bssid = dev->wl->bssid;
  582. mac = dev->wl->mac_addr;
  583. b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
  584. memcpy(mac_bssid, mac, ETH_ALEN);
  585. memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
  586. /* Write our MAC address and BSSID to template ram */
  587. for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
  588. tmp = (u32) (mac_bssid[i + 0]);
  589. tmp |= (u32) (mac_bssid[i + 1]) << 8;
  590. tmp |= (u32) (mac_bssid[i + 2]) << 16;
  591. tmp |= (u32) (mac_bssid[i + 3]) << 24;
  592. b43_ram_write(dev, 0x20 + i, tmp);
  593. }
  594. }
  595. static void b43_upload_card_macaddress(struct b43_wldev *dev)
  596. {
  597. b43_write_mac_bssid_templates(dev);
  598. b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
  599. }
  600. static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
  601. {
  602. /* slot_time is in usec. */
  603. if (dev->phy.type != B43_PHYTYPE_G)
  604. return;
  605. b43_write16(dev, 0x684, 510 + slot_time);
  606. b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
  607. }
  608. static void b43_short_slot_timing_enable(struct b43_wldev *dev)
  609. {
  610. b43_set_slot_time(dev, 9);
  611. }
  612. static void b43_short_slot_timing_disable(struct b43_wldev *dev)
  613. {
  614. b43_set_slot_time(dev, 20);
  615. }
  616. /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
  617. * Returns the _previously_ enabled IRQ mask.
  618. */
  619. static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
  620. {
  621. u32 old_mask;
  622. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  623. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
  624. return old_mask;
  625. }
  626. /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
  627. * Returns the _previously_ enabled IRQ mask.
  628. */
  629. static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
  630. {
  631. u32 old_mask;
  632. old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  633. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
  634. return old_mask;
  635. }
  636. /* Synchronize IRQ top- and bottom-half.
  637. * IRQs must be masked before calling this.
  638. * This must not be called with the irq_lock held.
  639. */
  640. static void b43_synchronize_irq(struct b43_wldev *dev)
  641. {
  642. synchronize_irq(dev->dev->irq);
  643. tasklet_kill(&dev->isr_tasklet);
  644. }
  645. /* DummyTransmission function, as documented on
  646. * http://bcm-specs.sipsolutions.net/DummyTransmission
  647. */
  648. void b43_dummy_transmission(struct b43_wldev *dev)
  649. {
  650. struct b43_wl *wl = dev->wl;
  651. struct b43_phy *phy = &dev->phy;
  652. unsigned int i, max_loop;
  653. u16 value;
  654. u32 buffer[5] = {
  655. 0x00000000,
  656. 0x00D40000,
  657. 0x00000000,
  658. 0x01000000,
  659. 0x00000000,
  660. };
  661. switch (phy->type) {
  662. case B43_PHYTYPE_A:
  663. max_loop = 0x1E;
  664. buffer[0] = 0x000201CC;
  665. break;
  666. case B43_PHYTYPE_B:
  667. case B43_PHYTYPE_G:
  668. max_loop = 0xFA;
  669. buffer[0] = 0x000B846E;
  670. break;
  671. default:
  672. B43_WARN_ON(1);
  673. return;
  674. }
  675. spin_lock_irq(&wl->irq_lock);
  676. write_lock(&wl->tx_lock);
  677. for (i = 0; i < 5; i++)
  678. b43_ram_write(dev, i * 4, buffer[i]);
  679. /* Commit writes */
  680. b43_read32(dev, B43_MMIO_MACCTL);
  681. b43_write16(dev, 0x0568, 0x0000);
  682. b43_write16(dev, 0x07C0, 0x0000);
  683. value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
  684. b43_write16(dev, 0x050C, value);
  685. b43_write16(dev, 0x0508, 0x0000);
  686. b43_write16(dev, 0x050A, 0x0000);
  687. b43_write16(dev, 0x054C, 0x0000);
  688. b43_write16(dev, 0x056A, 0x0014);
  689. b43_write16(dev, 0x0568, 0x0826);
  690. b43_write16(dev, 0x0500, 0x0000);
  691. b43_write16(dev, 0x0502, 0x0030);
  692. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  693. b43_radio_write16(dev, 0x0051, 0x0017);
  694. for (i = 0x00; i < max_loop; i++) {
  695. value = b43_read16(dev, 0x050E);
  696. if (value & 0x0080)
  697. break;
  698. udelay(10);
  699. }
  700. for (i = 0x00; i < 0x0A; i++) {
  701. value = b43_read16(dev, 0x050E);
  702. if (value & 0x0400)
  703. break;
  704. udelay(10);
  705. }
  706. for (i = 0x00; i < 0x19; i++) {
  707. value = b43_read16(dev, 0x0690);
  708. if (!(value & 0x0100))
  709. break;
  710. udelay(10);
  711. }
  712. if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
  713. b43_radio_write16(dev, 0x0051, 0x0037);
  714. write_unlock(&wl->tx_lock);
  715. spin_unlock_irq(&wl->irq_lock);
  716. }
  717. static void key_write(struct b43_wldev *dev,
  718. u8 index, u8 algorithm, const u8 * key)
  719. {
  720. unsigned int i;
  721. u32 offset;
  722. u16 value;
  723. u16 kidx;
  724. /* Key index/algo block */
  725. kidx = b43_kidx_to_fw(dev, index);
  726. value = ((kidx << 4) | algorithm);
  727. b43_shm_write16(dev, B43_SHM_SHARED,
  728. B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
  729. /* Write the key to the Key Table Pointer offset */
  730. offset = dev->ktp + (index * B43_SEC_KEYSIZE);
  731. for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
  732. value = key[i];
  733. value |= (u16) (key[i + 1]) << 8;
  734. b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
  735. }
  736. }
  737. static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
  738. {
  739. u32 addrtmp[2] = { 0, 0, };
  740. u8 per_sta_keys_start = 8;
  741. if (b43_new_kidx_api(dev))
  742. per_sta_keys_start = 4;
  743. B43_WARN_ON(index < per_sta_keys_start);
  744. /* We have two default TX keys and possibly two default RX keys.
  745. * Physical mac 0 is mapped to physical key 4 or 8, depending
  746. * on the firmware version.
  747. * So we must adjust the index here.
  748. */
  749. index -= per_sta_keys_start;
  750. if (addr) {
  751. addrtmp[0] = addr[0];
  752. addrtmp[0] |= ((u32) (addr[1]) << 8);
  753. addrtmp[0] |= ((u32) (addr[2]) << 16);
  754. addrtmp[0] |= ((u32) (addr[3]) << 24);
  755. addrtmp[1] = addr[4];
  756. addrtmp[1] |= ((u32) (addr[5]) << 8);
  757. }
  758. if (dev->dev->id.revision >= 5) {
  759. /* Receive match transmitter address mechanism */
  760. b43_shm_write32(dev, B43_SHM_RCMTA,
  761. (index * 2) + 0, addrtmp[0]);
  762. b43_shm_write16(dev, B43_SHM_RCMTA,
  763. (index * 2) + 1, addrtmp[1]);
  764. } else {
  765. /* RXE (Receive Engine) and
  766. * PSM (Programmable State Machine) mechanism
  767. */
  768. if (index < 8) {
  769. /* TODO write to RCM 16, 19, 22 and 25 */
  770. } else {
  771. b43_shm_write32(dev, B43_SHM_SHARED,
  772. B43_SHM_SH_PSM + (index * 6) + 0,
  773. addrtmp[0]);
  774. b43_shm_write16(dev, B43_SHM_SHARED,
  775. B43_SHM_SH_PSM + (index * 6) + 4,
  776. addrtmp[1]);
  777. }
  778. }
  779. }
  780. static void do_key_write(struct b43_wldev *dev,
  781. u8 index, u8 algorithm,
  782. const u8 * key, size_t key_len, const u8 * mac_addr)
  783. {
  784. u8 buf[B43_SEC_KEYSIZE] = { 0, };
  785. u8 per_sta_keys_start = 8;
  786. if (b43_new_kidx_api(dev))
  787. per_sta_keys_start = 4;
  788. B43_WARN_ON(index >= dev->max_nr_keys);
  789. B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
  790. if (index >= per_sta_keys_start)
  791. keymac_write(dev, index, NULL); /* First zero out mac. */
  792. if (key)
  793. memcpy(buf, key, key_len);
  794. key_write(dev, index, algorithm, buf);
  795. if (index >= per_sta_keys_start)
  796. keymac_write(dev, index, mac_addr);
  797. dev->key[index].algorithm = algorithm;
  798. }
  799. static int b43_key_write(struct b43_wldev *dev,
  800. int index, u8 algorithm,
  801. const u8 * key, size_t key_len,
  802. const u8 * mac_addr,
  803. struct ieee80211_key_conf *keyconf)
  804. {
  805. int i;
  806. int sta_keys_start;
  807. if (key_len > B43_SEC_KEYSIZE)
  808. return -EINVAL;
  809. for (i = 0; i < dev->max_nr_keys; i++) {
  810. /* Check that we don't already have this key. */
  811. B43_WARN_ON(dev->key[i].keyconf == keyconf);
  812. }
  813. if (index < 0) {
  814. /* Either pairwise key or address is 00:00:00:00:00:00
  815. * for transmit-only keys. Search the index. */
  816. if (b43_new_kidx_api(dev))
  817. sta_keys_start = 4;
  818. else
  819. sta_keys_start = 8;
  820. for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
  821. if (!dev->key[i].keyconf) {
  822. /* found empty */
  823. index = i;
  824. break;
  825. }
  826. }
  827. if (index < 0) {
  828. b43err(dev->wl, "Out of hardware key memory\n");
  829. return -ENOSPC;
  830. }
  831. } else
  832. B43_WARN_ON(index > 3);
  833. do_key_write(dev, index, algorithm, key, key_len, mac_addr);
  834. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  835. /* Default RX key */
  836. B43_WARN_ON(mac_addr);
  837. do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
  838. }
  839. keyconf->hw_key_idx = index;
  840. dev->key[index].keyconf = keyconf;
  841. return 0;
  842. }
  843. static int b43_key_clear(struct b43_wldev *dev, int index)
  844. {
  845. if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
  846. return -EINVAL;
  847. do_key_write(dev, index, B43_SEC_ALGO_NONE,
  848. NULL, B43_SEC_KEYSIZE, NULL);
  849. if ((index <= 3) && !b43_new_kidx_api(dev)) {
  850. do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
  851. NULL, B43_SEC_KEYSIZE, NULL);
  852. }
  853. dev->key[index].keyconf = NULL;
  854. return 0;
  855. }
  856. static void b43_clear_keys(struct b43_wldev *dev)
  857. {
  858. int i;
  859. for (i = 0; i < dev->max_nr_keys; i++)
  860. b43_key_clear(dev, i);
  861. }
  862. void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
  863. {
  864. u32 macctl;
  865. u16 ucstat;
  866. bool hwps;
  867. bool awake;
  868. int i;
  869. B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
  870. (ps_flags & B43_PS_DISABLED));
  871. B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
  872. if (ps_flags & B43_PS_ENABLED) {
  873. hwps = 1;
  874. } else if (ps_flags & B43_PS_DISABLED) {
  875. hwps = 0;
  876. } else {
  877. //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
  878. // and thus is not an AP and we are associated, set bit 25
  879. }
  880. if (ps_flags & B43_PS_AWAKE) {
  881. awake = 1;
  882. } else if (ps_flags & B43_PS_ASLEEP) {
  883. awake = 0;
  884. } else {
  885. //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
  886. // or we are associated, or FIXME, or the latest PS-Poll packet sent was
  887. // successful, set bit26
  888. }
  889. /* FIXME: For now we force awake-on and hwps-off */
  890. hwps = 0;
  891. awake = 1;
  892. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  893. if (hwps)
  894. macctl |= B43_MACCTL_HWPS;
  895. else
  896. macctl &= ~B43_MACCTL_HWPS;
  897. if (awake)
  898. macctl |= B43_MACCTL_AWAKE;
  899. else
  900. macctl &= ~B43_MACCTL_AWAKE;
  901. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  902. /* Commit write */
  903. b43_read32(dev, B43_MMIO_MACCTL);
  904. if (awake && dev->dev->id.revision >= 5) {
  905. /* Wait for the microcode to wake up. */
  906. for (i = 0; i < 100; i++) {
  907. ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
  908. B43_SHM_SH_UCODESTAT);
  909. if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
  910. break;
  911. udelay(10);
  912. }
  913. }
  914. }
  915. void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
  916. {
  917. u32 tmslow;
  918. u32 macctl;
  919. flags |= B43_TMSLOW_PHYCLKEN;
  920. flags |= B43_TMSLOW_PHYRESET;
  921. ssb_device_enable(dev->dev, flags);
  922. msleep(2); /* Wait for the PLL to turn on. */
  923. /* Now take the PHY out of Reset again */
  924. tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
  925. tmslow |= SSB_TMSLOW_FGC;
  926. tmslow &= ~B43_TMSLOW_PHYRESET;
  927. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  928. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  929. msleep(1);
  930. tmslow &= ~SSB_TMSLOW_FGC;
  931. ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
  932. ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
  933. msleep(1);
  934. /* Turn Analog ON, but only if we already know the PHY-type.
  935. * This protects against very early setup where we don't know the
  936. * PHY-type, yet. wireless_core_reset will be called once again later,
  937. * when we know the PHY-type. */
  938. if (dev->phy.ops)
  939. dev->phy.ops->switch_analog(dev, 1);
  940. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  941. macctl &= ~B43_MACCTL_GMODE;
  942. if (flags & B43_TMSLOW_GMODE)
  943. macctl |= B43_MACCTL_GMODE;
  944. macctl |= B43_MACCTL_IHR_ENABLED;
  945. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  946. }
  947. static void handle_irq_transmit_status(struct b43_wldev *dev)
  948. {
  949. u32 v0, v1;
  950. u16 tmp;
  951. struct b43_txstatus stat;
  952. while (1) {
  953. v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  954. if (!(v0 & 0x00000001))
  955. break;
  956. v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  957. stat.cookie = (v0 >> 16);
  958. stat.seq = (v1 & 0x0000FFFF);
  959. stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
  960. tmp = (v0 & 0x0000FFFF);
  961. stat.frame_count = ((tmp & 0xF000) >> 12);
  962. stat.rts_count = ((tmp & 0x0F00) >> 8);
  963. stat.supp_reason = ((tmp & 0x001C) >> 2);
  964. stat.pm_indicated = !!(tmp & 0x0080);
  965. stat.intermediate = !!(tmp & 0x0040);
  966. stat.for_ampdu = !!(tmp & 0x0020);
  967. stat.acked = !!(tmp & 0x0002);
  968. b43_handle_txstatus(dev, &stat);
  969. }
  970. }
  971. static void drain_txstatus_queue(struct b43_wldev *dev)
  972. {
  973. u32 dummy;
  974. if (dev->dev->id.revision < 5)
  975. return;
  976. /* Read all entries from the microcode TXstatus FIFO
  977. * and throw them away.
  978. */
  979. while (1) {
  980. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
  981. if (!(dummy & 0x00000001))
  982. break;
  983. dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
  984. }
  985. }
  986. static u32 b43_jssi_read(struct b43_wldev *dev)
  987. {
  988. u32 val = 0;
  989. val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
  990. val <<= 16;
  991. val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
  992. return val;
  993. }
  994. static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
  995. {
  996. b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
  997. b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
  998. }
  999. static void b43_generate_noise_sample(struct b43_wldev *dev)
  1000. {
  1001. b43_jssi_write(dev, 0x7F7F7F7F);
  1002. b43_write32(dev, B43_MMIO_MACCMD,
  1003. b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
  1004. }
  1005. static void b43_calculate_link_quality(struct b43_wldev *dev)
  1006. {
  1007. /* Top half of Link Quality calculation. */
  1008. if (dev->phy.type != B43_PHYTYPE_G)
  1009. return;
  1010. if (dev->noisecalc.calculation_running)
  1011. return;
  1012. dev->noisecalc.calculation_running = 1;
  1013. dev->noisecalc.nr_samples = 0;
  1014. b43_generate_noise_sample(dev);
  1015. }
  1016. static void handle_irq_noise(struct b43_wldev *dev)
  1017. {
  1018. struct b43_phy_g *phy = dev->phy.g;
  1019. u16 tmp;
  1020. u8 noise[4];
  1021. u8 i, j;
  1022. s32 average;
  1023. /* Bottom half of Link Quality calculation. */
  1024. if (dev->phy.type != B43_PHYTYPE_G)
  1025. return;
  1026. /* Possible race condition: It might be possible that the user
  1027. * changed to a different channel in the meantime since we
  1028. * started the calculation. We ignore that fact, since it's
  1029. * not really that much of a problem. The background noise is
  1030. * an estimation only anyway. Slightly wrong results will get damped
  1031. * by the averaging of the 8 sample rounds. Additionally the
  1032. * value is shortlived. So it will be replaced by the next noise
  1033. * calculation round soon. */
  1034. B43_WARN_ON(!dev->noisecalc.calculation_running);
  1035. *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
  1036. if (noise[0] == 0x7F || noise[1] == 0x7F ||
  1037. noise[2] == 0x7F || noise[3] == 0x7F)
  1038. goto generate_new;
  1039. /* Get the noise samples. */
  1040. B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
  1041. i = dev->noisecalc.nr_samples;
  1042. noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1043. noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1044. noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1045. noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
  1046. dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
  1047. dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
  1048. dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
  1049. dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
  1050. dev->noisecalc.nr_samples++;
  1051. if (dev->noisecalc.nr_samples == 8) {
  1052. /* Calculate the Link Quality by the noise samples. */
  1053. average = 0;
  1054. for (i = 0; i < 8; i++) {
  1055. for (j = 0; j < 4; j++)
  1056. average += dev->noisecalc.samples[i][j];
  1057. }
  1058. average /= (8 * 4);
  1059. average *= 125;
  1060. average += 64;
  1061. average /= 128;
  1062. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
  1063. tmp = (tmp / 128) & 0x1F;
  1064. if (tmp >= 8)
  1065. average += 2;
  1066. else
  1067. average -= 25;
  1068. if (tmp == 8)
  1069. average -= 72;
  1070. else
  1071. average -= 48;
  1072. dev->stats.link_noise = average;
  1073. dev->noisecalc.calculation_running = 0;
  1074. return;
  1075. }
  1076. generate_new:
  1077. b43_generate_noise_sample(dev);
  1078. }
  1079. static void handle_irq_tbtt_indication(struct b43_wldev *dev)
  1080. {
  1081. if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
  1082. ///TODO: PS TBTT
  1083. } else {
  1084. if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
  1085. b43_power_saving_ctl_bits(dev, 0);
  1086. }
  1087. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
  1088. dev->dfq_valid = 1;
  1089. }
  1090. static void handle_irq_atim_end(struct b43_wldev *dev)
  1091. {
  1092. if (dev->dfq_valid) {
  1093. b43_write32(dev, B43_MMIO_MACCMD,
  1094. b43_read32(dev, B43_MMIO_MACCMD)
  1095. | B43_MACCMD_DFQ_VALID);
  1096. dev->dfq_valid = 0;
  1097. }
  1098. }
  1099. static void handle_irq_pmq(struct b43_wldev *dev)
  1100. {
  1101. u32 tmp;
  1102. //TODO: AP mode.
  1103. while (1) {
  1104. tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
  1105. if (!(tmp & 0x00000008))
  1106. break;
  1107. }
  1108. /* 16bit write is odd, but correct. */
  1109. b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
  1110. }
  1111. static void b43_write_template_common(struct b43_wldev *dev,
  1112. const u8 * data, u16 size,
  1113. u16 ram_offset,
  1114. u16 shm_size_offset, u8 rate)
  1115. {
  1116. u32 i, tmp;
  1117. struct b43_plcp_hdr4 plcp;
  1118. plcp.data = 0;
  1119. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
  1120. b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
  1121. ram_offset += sizeof(u32);
  1122. /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
  1123. * So leave the first two bytes of the next write blank.
  1124. */
  1125. tmp = (u32) (data[0]) << 16;
  1126. tmp |= (u32) (data[1]) << 24;
  1127. b43_ram_write(dev, ram_offset, tmp);
  1128. ram_offset += sizeof(u32);
  1129. for (i = 2; i < size; i += sizeof(u32)) {
  1130. tmp = (u32) (data[i + 0]);
  1131. if (i + 1 < size)
  1132. tmp |= (u32) (data[i + 1]) << 8;
  1133. if (i + 2 < size)
  1134. tmp |= (u32) (data[i + 2]) << 16;
  1135. if (i + 3 < size)
  1136. tmp |= (u32) (data[i + 3]) << 24;
  1137. b43_ram_write(dev, ram_offset + i - 2, tmp);
  1138. }
  1139. b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
  1140. size + sizeof(struct b43_plcp_hdr6));
  1141. }
  1142. /* Check if the use of the antenna that ieee80211 told us to
  1143. * use is possible. This will fall back to DEFAULT.
  1144. * "antenna_nr" is the antenna identifier we got from ieee80211. */
  1145. u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
  1146. u8 antenna_nr)
  1147. {
  1148. u8 antenna_mask;
  1149. if (antenna_nr == 0) {
  1150. /* Zero means "use default antenna". That's always OK. */
  1151. return 0;
  1152. }
  1153. /* Get the mask of available antennas. */
  1154. if (dev->phy.gmode)
  1155. antenna_mask = dev->dev->bus->sprom.ant_available_bg;
  1156. else
  1157. antenna_mask = dev->dev->bus->sprom.ant_available_a;
  1158. if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
  1159. /* This antenna is not available. Fall back to default. */
  1160. return 0;
  1161. }
  1162. return antenna_nr;
  1163. }
  1164. /* Convert a b43 antenna number value to the PHY TX control value. */
  1165. static u16 b43_antenna_to_phyctl(int antenna)
  1166. {
  1167. switch (antenna) {
  1168. case B43_ANTENNA0:
  1169. return B43_TXH_PHY_ANT0;
  1170. case B43_ANTENNA1:
  1171. return B43_TXH_PHY_ANT1;
  1172. case B43_ANTENNA2:
  1173. return B43_TXH_PHY_ANT2;
  1174. case B43_ANTENNA3:
  1175. return B43_TXH_PHY_ANT3;
  1176. case B43_ANTENNA_AUTO:
  1177. return B43_TXH_PHY_ANT01AUTO;
  1178. }
  1179. B43_WARN_ON(1);
  1180. return 0;
  1181. }
  1182. static void b43_write_beacon_template(struct b43_wldev *dev,
  1183. u16 ram_offset,
  1184. u16 shm_size_offset)
  1185. {
  1186. unsigned int i, len, variable_len;
  1187. const struct ieee80211_mgmt *bcn;
  1188. const u8 *ie;
  1189. bool tim_found = 0;
  1190. unsigned int rate;
  1191. u16 ctl;
  1192. int antenna;
  1193. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
  1194. bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
  1195. len = min((size_t) dev->wl->current_beacon->len,
  1196. 0x200 - sizeof(struct b43_plcp_hdr6));
  1197. rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
  1198. b43_write_template_common(dev, (const u8 *)bcn,
  1199. len, ram_offset, shm_size_offset, rate);
  1200. /* Write the PHY TX control parameters. */
  1201. antenna = B43_ANTENNA_DEFAULT;
  1202. antenna = b43_antenna_to_phyctl(antenna);
  1203. ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
  1204. /* We can't send beacons with short preamble. Would get PHY errors. */
  1205. ctl &= ~B43_TXH_PHY_SHORTPRMBL;
  1206. ctl &= ~B43_TXH_PHY_ANT;
  1207. ctl &= ~B43_TXH_PHY_ENC;
  1208. ctl |= antenna;
  1209. if (b43_is_cck_rate(rate))
  1210. ctl |= B43_TXH_PHY_ENC_CCK;
  1211. else
  1212. ctl |= B43_TXH_PHY_ENC_OFDM;
  1213. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  1214. /* Find the position of the TIM and the DTIM_period value
  1215. * and write them to SHM. */
  1216. ie = bcn->u.beacon.variable;
  1217. variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1218. for (i = 0; i < variable_len - 2; ) {
  1219. uint8_t ie_id, ie_len;
  1220. ie_id = ie[i];
  1221. ie_len = ie[i + 1];
  1222. if (ie_id == 5) {
  1223. u16 tim_position;
  1224. u16 dtim_period;
  1225. /* This is the TIM Information Element */
  1226. /* Check whether the ie_len is in the beacon data range. */
  1227. if (variable_len < ie_len + 2 + i)
  1228. break;
  1229. /* A valid TIM is at least 4 bytes long. */
  1230. if (ie_len < 4)
  1231. break;
  1232. tim_found = 1;
  1233. tim_position = sizeof(struct b43_plcp_hdr6);
  1234. tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1235. tim_position += i;
  1236. dtim_period = ie[i + 3];
  1237. b43_shm_write16(dev, B43_SHM_SHARED,
  1238. B43_SHM_SH_TIMBPOS, tim_position);
  1239. b43_shm_write16(dev, B43_SHM_SHARED,
  1240. B43_SHM_SH_DTIMPER, dtim_period);
  1241. break;
  1242. }
  1243. i += ie_len + 2;
  1244. }
  1245. if (!tim_found) {
  1246. /*
  1247. * If ucode wants to modify TIM do it behind the beacon, this
  1248. * will happen, for example, when doing mesh networking.
  1249. */
  1250. b43_shm_write16(dev, B43_SHM_SHARED,
  1251. B43_SHM_SH_TIMBPOS,
  1252. len + sizeof(struct b43_plcp_hdr6));
  1253. b43_shm_write16(dev, B43_SHM_SHARED,
  1254. B43_SHM_SH_DTIMPER, 0);
  1255. }
  1256. b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
  1257. }
  1258. static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  1259. u16 shm_offset, u16 size,
  1260. struct ieee80211_rate *rate)
  1261. {
  1262. struct b43_plcp_hdr4 plcp;
  1263. u32 tmp;
  1264. __le16 dur;
  1265. plcp.data = 0;
  1266. b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
  1267. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1268. dev->wl->vif, size,
  1269. rate);
  1270. /* Write PLCP in two parts and timing for packet transfer */
  1271. tmp = le32_to_cpu(plcp.data);
  1272. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
  1273. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
  1274. b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
  1275. }
  1276. /* Instead of using custom probe response template, this function
  1277. * just patches custom beacon template by:
  1278. * 1) Changing packet type
  1279. * 2) Patching duration field
  1280. * 3) Stripping TIM
  1281. */
  1282. static const u8 * b43_generate_probe_resp(struct b43_wldev *dev,
  1283. u16 *dest_size,
  1284. struct ieee80211_rate *rate)
  1285. {
  1286. const u8 *src_data;
  1287. u8 *dest_data;
  1288. u16 src_size, elem_size, src_pos, dest_pos;
  1289. __le16 dur;
  1290. struct ieee80211_hdr *hdr;
  1291. size_t ie_start;
  1292. src_size = dev->wl->current_beacon->len;
  1293. src_data = (const u8 *)dev->wl->current_beacon->data;
  1294. /* Get the start offset of the variable IEs in the packet. */
  1295. ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  1296. B43_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1297. if (B43_WARN_ON(src_size < ie_start))
  1298. return NULL;
  1299. dest_data = kmalloc(src_size, GFP_ATOMIC);
  1300. if (unlikely(!dest_data))
  1301. return NULL;
  1302. /* Copy the static data and all Information Elements, except the TIM. */
  1303. memcpy(dest_data, src_data, ie_start);
  1304. src_pos = ie_start;
  1305. dest_pos = ie_start;
  1306. for ( ; src_pos < src_size - 2; src_pos += elem_size) {
  1307. elem_size = src_data[src_pos + 1] + 2;
  1308. if (src_data[src_pos] == 5) {
  1309. /* This is the TIM. */
  1310. continue;
  1311. }
  1312. memcpy(dest_data + dest_pos, src_data + src_pos,
  1313. elem_size);
  1314. dest_pos += elem_size;
  1315. }
  1316. *dest_size = dest_pos;
  1317. hdr = (struct ieee80211_hdr *)dest_data;
  1318. /* Set the frame control. */
  1319. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1320. IEEE80211_STYPE_PROBE_RESP);
  1321. dur = ieee80211_generic_frame_duration(dev->wl->hw,
  1322. dev->wl->vif, *dest_size,
  1323. rate);
  1324. hdr->duration_id = dur;
  1325. return dest_data;
  1326. }
  1327. static void b43_write_probe_resp_template(struct b43_wldev *dev,
  1328. u16 ram_offset,
  1329. u16 shm_size_offset,
  1330. struct ieee80211_rate *rate)
  1331. {
  1332. const u8 *probe_resp_data;
  1333. u16 size;
  1334. size = dev->wl->current_beacon->len;
  1335. probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
  1336. if (unlikely(!probe_resp_data))
  1337. return;
  1338. /* Looks like PLCP headers plus packet timings are stored for
  1339. * all possible basic rates
  1340. */
  1341. b43_write_probe_resp_plcp(dev, 0x31A, size, &b43_b_ratetable[0]);
  1342. b43_write_probe_resp_plcp(dev, 0x32C, size, &b43_b_ratetable[1]);
  1343. b43_write_probe_resp_plcp(dev, 0x33E, size, &b43_b_ratetable[2]);
  1344. b43_write_probe_resp_plcp(dev, 0x350, size, &b43_b_ratetable[3]);
  1345. size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
  1346. b43_write_template_common(dev, probe_resp_data,
  1347. size, ram_offset, shm_size_offset,
  1348. rate->hw_value);
  1349. kfree(probe_resp_data);
  1350. }
  1351. static void b43_upload_beacon0(struct b43_wldev *dev)
  1352. {
  1353. struct b43_wl *wl = dev->wl;
  1354. if (wl->beacon0_uploaded)
  1355. return;
  1356. b43_write_beacon_template(dev, 0x68, 0x18);
  1357. /* FIXME: Probe resp upload doesn't really belong here,
  1358. * but we don't use that feature anyway. */
  1359. b43_write_probe_resp_template(dev, 0x268, 0x4A,
  1360. &__b43_ratetable[3]);
  1361. wl->beacon0_uploaded = 1;
  1362. }
  1363. static void b43_upload_beacon1(struct b43_wldev *dev)
  1364. {
  1365. struct b43_wl *wl = dev->wl;
  1366. if (wl->beacon1_uploaded)
  1367. return;
  1368. b43_write_beacon_template(dev, 0x468, 0x1A);
  1369. wl->beacon1_uploaded = 1;
  1370. }
  1371. static void handle_irq_beacon(struct b43_wldev *dev)
  1372. {
  1373. struct b43_wl *wl = dev->wl;
  1374. u32 cmd, beacon0_valid, beacon1_valid;
  1375. if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
  1376. !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  1377. return;
  1378. /* This is the bottom half of the asynchronous beacon update. */
  1379. /* Ignore interrupt in the future. */
  1380. dev->irq_savedstate &= ~B43_IRQ_BEACON;
  1381. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1382. beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
  1383. beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
  1384. /* Schedule interrupt manually, if busy. */
  1385. if (beacon0_valid && beacon1_valid) {
  1386. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
  1387. dev->irq_savedstate |= B43_IRQ_BEACON;
  1388. return;
  1389. }
  1390. if (unlikely(wl->beacon_templates_virgin)) {
  1391. /* We never uploaded a beacon before.
  1392. * Upload both templates now, but only mark one valid. */
  1393. wl->beacon_templates_virgin = 0;
  1394. b43_upload_beacon0(dev);
  1395. b43_upload_beacon1(dev);
  1396. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1397. cmd |= B43_MACCMD_BEACON0_VALID;
  1398. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1399. } else {
  1400. if (!beacon0_valid) {
  1401. b43_upload_beacon0(dev);
  1402. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1403. cmd |= B43_MACCMD_BEACON0_VALID;
  1404. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1405. } else if (!beacon1_valid) {
  1406. b43_upload_beacon1(dev);
  1407. cmd = b43_read32(dev, B43_MMIO_MACCMD);
  1408. cmd |= B43_MACCMD_BEACON1_VALID;
  1409. b43_write32(dev, B43_MMIO_MACCMD, cmd);
  1410. }
  1411. }
  1412. }
  1413. static void b43_beacon_update_trigger_work(struct work_struct *work)
  1414. {
  1415. struct b43_wl *wl = container_of(work, struct b43_wl,
  1416. beacon_update_trigger);
  1417. struct b43_wldev *dev;
  1418. mutex_lock(&wl->mutex);
  1419. dev = wl->current_dev;
  1420. if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
  1421. spin_lock_irq(&wl->irq_lock);
  1422. /* update beacon right away or defer to irq */
  1423. dev->irq_savedstate = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1424. handle_irq_beacon(dev);
  1425. /* The handler might have updated the IRQ mask. */
  1426. b43_write32(dev, B43_MMIO_GEN_IRQ_MASK,
  1427. dev->irq_savedstate);
  1428. mmiowb();
  1429. spin_unlock_irq(&wl->irq_lock);
  1430. }
  1431. mutex_unlock(&wl->mutex);
  1432. }
  1433. /* Asynchronously update the packet templates in template RAM.
  1434. * Locking: Requires wl->irq_lock to be locked. */
  1435. static void b43_update_templates(struct b43_wl *wl)
  1436. {
  1437. struct sk_buff *beacon;
  1438. /* This is the top half of the ansynchronous beacon update.
  1439. * The bottom half is the beacon IRQ.
  1440. * Beacon update must be asynchronous to avoid sending an
  1441. * invalid beacon. This can happen for example, if the firmware
  1442. * transmits a beacon while we are updating it. */
  1443. /* We could modify the existing beacon and set the aid bit in
  1444. * the TIM field, but that would probably require resizing and
  1445. * moving of data within the beacon template.
  1446. * Simply request a new beacon and let mac80211 do the hard work. */
  1447. beacon = ieee80211_beacon_get(wl->hw, wl->vif);
  1448. if (unlikely(!beacon))
  1449. return;
  1450. if (wl->current_beacon)
  1451. dev_kfree_skb_any(wl->current_beacon);
  1452. wl->current_beacon = beacon;
  1453. wl->beacon0_uploaded = 0;
  1454. wl->beacon1_uploaded = 0;
  1455. queue_work(wl->hw->workqueue, &wl->beacon_update_trigger);
  1456. }
  1457. static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
  1458. {
  1459. b43_time_lock(dev);
  1460. if (dev->dev->id.revision >= 3) {
  1461. b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
  1462. b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
  1463. } else {
  1464. b43_write16(dev, 0x606, (beacon_int >> 6));
  1465. b43_write16(dev, 0x610, beacon_int);
  1466. }
  1467. b43_time_unlock(dev);
  1468. b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
  1469. }
  1470. static void b43_handle_firmware_panic(struct b43_wldev *dev)
  1471. {
  1472. u16 reason;
  1473. /* Read the register that contains the reason code for the panic. */
  1474. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
  1475. b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
  1476. switch (reason) {
  1477. default:
  1478. b43dbg(dev->wl, "The panic reason is unknown.\n");
  1479. /* fallthrough */
  1480. case B43_FWPANIC_DIE:
  1481. /* Do not restart the controller or firmware.
  1482. * The device is nonfunctional from now on.
  1483. * Restarting would result in this panic to trigger again,
  1484. * so we avoid that recursion. */
  1485. break;
  1486. case B43_FWPANIC_RESTART:
  1487. b43_controller_restart(dev, "Microcode panic");
  1488. break;
  1489. }
  1490. }
  1491. static void handle_irq_ucode_debug(struct b43_wldev *dev)
  1492. {
  1493. unsigned int i, cnt;
  1494. u16 reason, marker_id, marker_line;
  1495. __le16 *buf;
  1496. /* The proprietary firmware doesn't have this IRQ. */
  1497. if (!dev->fw.opensource)
  1498. return;
  1499. /* Read the register that contains the reason code for this IRQ. */
  1500. reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
  1501. switch (reason) {
  1502. case B43_DEBUGIRQ_PANIC:
  1503. b43_handle_firmware_panic(dev);
  1504. break;
  1505. case B43_DEBUGIRQ_DUMP_SHM:
  1506. if (!B43_DEBUG)
  1507. break; /* Only with driver debugging enabled. */
  1508. buf = kmalloc(4096, GFP_ATOMIC);
  1509. if (!buf) {
  1510. b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
  1511. goto out;
  1512. }
  1513. for (i = 0; i < 4096; i += 2) {
  1514. u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
  1515. buf[i / 2] = cpu_to_le16(tmp);
  1516. }
  1517. b43info(dev->wl, "Shared memory dump:\n");
  1518. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
  1519. 16, 2, buf, 4096, 1);
  1520. kfree(buf);
  1521. break;
  1522. case B43_DEBUGIRQ_DUMP_REGS:
  1523. if (!B43_DEBUG)
  1524. break; /* Only with driver debugging enabled. */
  1525. b43info(dev->wl, "Microcode register dump:\n");
  1526. for (i = 0, cnt = 0; i < 64; i++) {
  1527. u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
  1528. if (cnt == 0)
  1529. printk(KERN_INFO);
  1530. printk("r%02u: 0x%04X ", i, tmp);
  1531. cnt++;
  1532. if (cnt == 6) {
  1533. printk("\n");
  1534. cnt = 0;
  1535. }
  1536. }
  1537. printk("\n");
  1538. break;
  1539. case B43_DEBUGIRQ_MARKER:
  1540. if (!B43_DEBUG)
  1541. break; /* Only with driver debugging enabled. */
  1542. marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1543. B43_MARKER_ID_REG);
  1544. marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
  1545. B43_MARKER_LINE_REG);
  1546. b43info(dev->wl, "The firmware just executed the MARKER(%u) "
  1547. "at line number %u\n",
  1548. marker_id, marker_line);
  1549. break;
  1550. default:
  1551. b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
  1552. reason);
  1553. }
  1554. out:
  1555. /* Acknowledge the debug-IRQ, so the firmware can continue. */
  1556. b43_shm_write16(dev, B43_SHM_SCRATCH,
  1557. B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
  1558. }
  1559. /* Interrupt handler bottom-half */
  1560. static void b43_interrupt_tasklet(struct b43_wldev *dev)
  1561. {
  1562. u32 reason;
  1563. u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
  1564. u32 merged_dma_reason = 0;
  1565. int i;
  1566. unsigned long flags;
  1567. spin_lock_irqsave(&dev->wl->irq_lock, flags);
  1568. B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
  1569. reason = dev->irq_reason;
  1570. for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
  1571. dma_reason[i] = dev->dma_reason[i];
  1572. merged_dma_reason |= dma_reason[i];
  1573. }
  1574. if (unlikely(reason & B43_IRQ_MAC_TXERR))
  1575. b43err(dev->wl, "MAC transmission error\n");
  1576. if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
  1577. b43err(dev->wl, "PHY transmission error\n");
  1578. rmb();
  1579. if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
  1580. atomic_set(&dev->phy.txerr_cnt,
  1581. B43_PHY_TX_BADNESS_LIMIT);
  1582. b43err(dev->wl, "Too many PHY TX errors, "
  1583. "restarting the controller\n");
  1584. b43_controller_restart(dev, "PHY TX errors");
  1585. }
  1586. }
  1587. if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
  1588. B43_DMAIRQ_NONFATALMASK))) {
  1589. if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
  1590. b43err(dev->wl, "Fatal DMA error: "
  1591. "0x%08X, 0x%08X, 0x%08X, "
  1592. "0x%08X, 0x%08X, 0x%08X\n",
  1593. dma_reason[0], dma_reason[1],
  1594. dma_reason[2], dma_reason[3],
  1595. dma_reason[4], dma_reason[5]);
  1596. b43_controller_restart(dev, "DMA error");
  1597. mmiowb();
  1598. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1599. return;
  1600. }
  1601. if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
  1602. b43err(dev->wl, "DMA error: "
  1603. "0x%08X, 0x%08X, 0x%08X, "
  1604. "0x%08X, 0x%08X, 0x%08X\n",
  1605. dma_reason[0], dma_reason[1],
  1606. dma_reason[2], dma_reason[3],
  1607. dma_reason[4], dma_reason[5]);
  1608. }
  1609. }
  1610. if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
  1611. handle_irq_ucode_debug(dev);
  1612. if (reason & B43_IRQ_TBTT_INDI)
  1613. handle_irq_tbtt_indication(dev);
  1614. if (reason & B43_IRQ_ATIM_END)
  1615. handle_irq_atim_end(dev);
  1616. if (reason & B43_IRQ_BEACON)
  1617. handle_irq_beacon(dev);
  1618. if (reason & B43_IRQ_PMQ)
  1619. handle_irq_pmq(dev);
  1620. if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
  1621. ;/* TODO */
  1622. if (reason & B43_IRQ_NOISESAMPLE_OK)
  1623. handle_irq_noise(dev);
  1624. /* Check the DMA reason registers for received data. */
  1625. if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
  1626. if (b43_using_pio_transfers(dev))
  1627. b43_pio_rx(dev->pio.rx_queue);
  1628. else
  1629. b43_dma_rx(dev->dma.rx_ring);
  1630. }
  1631. B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
  1632. B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
  1633. B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
  1634. B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
  1635. B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
  1636. if (reason & B43_IRQ_TX_OK)
  1637. handle_irq_transmit_status(dev);
  1638. b43_interrupt_enable(dev, dev->irq_savedstate);
  1639. mmiowb();
  1640. spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
  1641. }
  1642. static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
  1643. {
  1644. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
  1645. b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
  1646. b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
  1647. b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
  1648. b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
  1649. b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
  1650. b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
  1651. }
  1652. /* Interrupt handler top-half */
  1653. static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
  1654. {
  1655. irqreturn_t ret = IRQ_NONE;
  1656. struct b43_wldev *dev = dev_id;
  1657. u32 reason;
  1658. if (!dev)
  1659. return IRQ_NONE;
  1660. spin_lock(&dev->wl->irq_lock);
  1661. if (b43_status(dev) < B43_STAT_STARTED)
  1662. goto out;
  1663. reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1664. if (reason == 0xffffffff) /* shared IRQ */
  1665. goto out;
  1666. ret = IRQ_HANDLED;
  1667. reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
  1668. if (!reason)
  1669. goto out;
  1670. dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
  1671. & 0x0001DC00;
  1672. dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
  1673. & 0x0000DC00;
  1674. dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
  1675. & 0x0000DC00;
  1676. dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
  1677. & 0x0001DC00;
  1678. dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
  1679. & 0x0000DC00;
  1680. dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
  1681. & 0x0000DC00;
  1682. b43_interrupt_ack(dev, reason);
  1683. /* disable all IRQs. They are enabled again in the bottom half. */
  1684. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  1685. /* save the reason code and call our bottom half. */
  1686. dev->irq_reason = reason;
  1687. tasklet_schedule(&dev->isr_tasklet);
  1688. out:
  1689. mmiowb();
  1690. spin_unlock(&dev->wl->irq_lock);
  1691. return ret;
  1692. }
  1693. static void do_release_fw(struct b43_firmware_file *fw)
  1694. {
  1695. release_firmware(fw->data);
  1696. fw->data = NULL;
  1697. fw->filename = NULL;
  1698. }
  1699. static void b43_release_firmware(struct b43_wldev *dev)
  1700. {
  1701. do_release_fw(&dev->fw.ucode);
  1702. do_release_fw(&dev->fw.pcm);
  1703. do_release_fw(&dev->fw.initvals);
  1704. do_release_fw(&dev->fw.initvals_band);
  1705. }
  1706. static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
  1707. {
  1708. const char *text;
  1709. text = "You must go to "
  1710. "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
  1711. "and download the latest firmware (version 4).\n";
  1712. if (error)
  1713. b43err(wl, text);
  1714. else
  1715. b43warn(wl, text);
  1716. }
  1717. static int do_request_fw(struct b43_wldev *dev,
  1718. const char *name,
  1719. struct b43_firmware_file *fw,
  1720. bool silent)
  1721. {
  1722. char path[sizeof(modparam_fwpostfix) + 32];
  1723. const struct firmware *blob;
  1724. struct b43_fw_header *hdr;
  1725. u32 size;
  1726. int err;
  1727. if (!name) {
  1728. /* Don't fetch anything. Free possibly cached firmware. */
  1729. do_release_fw(fw);
  1730. return 0;
  1731. }
  1732. if (fw->filename) {
  1733. if (strcmp(fw->filename, name) == 0)
  1734. return 0; /* Already have this fw. */
  1735. /* Free the cached firmware first. */
  1736. do_release_fw(fw);
  1737. }
  1738. snprintf(path, ARRAY_SIZE(path),
  1739. "b43%s/%s.fw",
  1740. modparam_fwpostfix, name);
  1741. err = request_firmware(&blob, path, dev->dev->dev);
  1742. if (err == -ENOENT) {
  1743. if (!silent) {
  1744. b43err(dev->wl, "Firmware file \"%s\" not found\n",
  1745. path);
  1746. }
  1747. return err;
  1748. } else if (err) {
  1749. b43err(dev->wl, "Firmware file \"%s\" request failed (err=%d)\n",
  1750. path, err);
  1751. return err;
  1752. }
  1753. if (blob->size < sizeof(struct b43_fw_header))
  1754. goto err_format;
  1755. hdr = (struct b43_fw_header *)(blob->data);
  1756. switch (hdr->type) {
  1757. case B43_FW_TYPE_UCODE:
  1758. case B43_FW_TYPE_PCM:
  1759. size = be32_to_cpu(hdr->size);
  1760. if (size != blob->size - sizeof(struct b43_fw_header))
  1761. goto err_format;
  1762. /* fallthrough */
  1763. case B43_FW_TYPE_IV:
  1764. if (hdr->ver != 1)
  1765. goto err_format;
  1766. break;
  1767. default:
  1768. goto err_format;
  1769. }
  1770. fw->data = blob;
  1771. fw->filename = name;
  1772. return 0;
  1773. err_format:
  1774. b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
  1775. release_firmware(blob);
  1776. return -EPROTO;
  1777. }
  1778. static int b43_request_firmware(struct b43_wldev *dev)
  1779. {
  1780. struct b43_firmware *fw = &dev->fw;
  1781. const u8 rev = dev->dev->id.revision;
  1782. const char *filename;
  1783. u32 tmshigh;
  1784. int err;
  1785. /* Get microcode */
  1786. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  1787. if ((rev >= 5) && (rev <= 10))
  1788. filename = "ucode5";
  1789. else if ((rev >= 11) && (rev <= 12))
  1790. filename = "ucode11";
  1791. else if (rev >= 13)
  1792. filename = "ucode13";
  1793. else
  1794. goto err_no_ucode;
  1795. err = do_request_fw(dev, filename, &fw->ucode, 0);
  1796. if (err)
  1797. goto err_load;
  1798. /* Get PCM code */
  1799. if ((rev >= 5) && (rev <= 10))
  1800. filename = "pcm5";
  1801. else if (rev >= 11)
  1802. filename = NULL;
  1803. else
  1804. goto err_no_pcm;
  1805. fw->pcm_request_failed = 0;
  1806. err = do_request_fw(dev, filename, &fw->pcm, 1);
  1807. if (err == -ENOENT) {
  1808. /* We did not find a PCM file? Not fatal, but
  1809. * core rev <= 10 must do without hwcrypto then. */
  1810. fw->pcm_request_failed = 1;
  1811. } else if (err)
  1812. goto err_load;
  1813. /* Get initvals */
  1814. switch (dev->phy.type) {
  1815. case B43_PHYTYPE_A:
  1816. if ((rev >= 5) && (rev <= 10)) {
  1817. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1818. filename = "a0g1initvals5";
  1819. else
  1820. filename = "a0g0initvals5";
  1821. } else
  1822. goto err_no_initvals;
  1823. break;
  1824. case B43_PHYTYPE_G:
  1825. if ((rev >= 5) && (rev <= 10))
  1826. filename = "b0g0initvals5";
  1827. else if (rev >= 13)
  1828. filename = "b0g0initvals13";
  1829. else
  1830. goto err_no_initvals;
  1831. break;
  1832. case B43_PHYTYPE_N:
  1833. if ((rev >= 11) && (rev <= 12))
  1834. filename = "n0initvals11";
  1835. else
  1836. goto err_no_initvals;
  1837. break;
  1838. default:
  1839. goto err_no_initvals;
  1840. }
  1841. err = do_request_fw(dev, filename, &fw->initvals, 0);
  1842. if (err)
  1843. goto err_load;
  1844. /* Get bandswitch initvals */
  1845. switch (dev->phy.type) {
  1846. case B43_PHYTYPE_A:
  1847. if ((rev >= 5) && (rev <= 10)) {
  1848. if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
  1849. filename = "a0g1bsinitvals5";
  1850. else
  1851. filename = "a0g0bsinitvals5";
  1852. } else if (rev >= 11)
  1853. filename = NULL;
  1854. else
  1855. goto err_no_initvals;
  1856. break;
  1857. case B43_PHYTYPE_G:
  1858. if ((rev >= 5) && (rev <= 10))
  1859. filename = "b0g0bsinitvals5";
  1860. else if (rev >= 11)
  1861. filename = NULL;
  1862. else
  1863. goto err_no_initvals;
  1864. break;
  1865. case B43_PHYTYPE_N:
  1866. if ((rev >= 11) && (rev <= 12))
  1867. filename = "n0bsinitvals11";
  1868. else
  1869. goto err_no_initvals;
  1870. break;
  1871. default:
  1872. goto err_no_initvals;
  1873. }
  1874. err = do_request_fw(dev, filename, &fw->initvals_band, 0);
  1875. if (err)
  1876. goto err_load;
  1877. return 0;
  1878. err_load:
  1879. b43_print_fw_helptext(dev->wl, 1);
  1880. goto error;
  1881. err_no_ucode:
  1882. err = -ENODEV;
  1883. b43err(dev->wl, "No microcode available for core rev %u\n", rev);
  1884. goto error;
  1885. err_no_pcm:
  1886. err = -ENODEV;
  1887. b43err(dev->wl, "No PCM available for core rev %u\n", rev);
  1888. goto error;
  1889. err_no_initvals:
  1890. err = -ENODEV;
  1891. b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
  1892. "core rev %u\n", dev->phy.type, rev);
  1893. goto error;
  1894. error:
  1895. b43_release_firmware(dev);
  1896. return err;
  1897. }
  1898. static int b43_upload_microcode(struct b43_wldev *dev)
  1899. {
  1900. const size_t hdr_len = sizeof(struct b43_fw_header);
  1901. const __be32 *data;
  1902. unsigned int i, len;
  1903. u16 fwrev, fwpatch, fwdate, fwtime;
  1904. u32 tmp, macctl;
  1905. int err = 0;
  1906. /* Jump the microcode PSM to offset 0 */
  1907. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1908. B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
  1909. macctl |= B43_MACCTL_PSM_JMP0;
  1910. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1911. /* Zero out all microcode PSM registers and shared memory. */
  1912. for (i = 0; i < 64; i++)
  1913. b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
  1914. for (i = 0; i < 4096; i += 2)
  1915. b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
  1916. /* Upload Microcode. */
  1917. data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
  1918. len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
  1919. b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
  1920. for (i = 0; i < len; i++) {
  1921. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1922. udelay(10);
  1923. }
  1924. if (dev->fw.pcm.data) {
  1925. /* Upload PCM data. */
  1926. data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
  1927. len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
  1928. b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
  1929. b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
  1930. /* No need for autoinc bit in SHM_HW */
  1931. b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
  1932. for (i = 0; i < len; i++) {
  1933. b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
  1934. udelay(10);
  1935. }
  1936. }
  1937. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
  1938. /* Start the microcode PSM */
  1939. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  1940. macctl &= ~B43_MACCTL_PSM_JMP0;
  1941. macctl |= B43_MACCTL_PSM_RUN;
  1942. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  1943. /* Wait for the microcode to load and respond */
  1944. i = 0;
  1945. while (1) {
  1946. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  1947. if (tmp == B43_IRQ_MAC_SUSPENDED)
  1948. break;
  1949. i++;
  1950. if (i >= 20) {
  1951. b43err(dev->wl, "Microcode not responding\n");
  1952. b43_print_fw_helptext(dev->wl, 1);
  1953. err = -ENODEV;
  1954. goto error;
  1955. }
  1956. msleep_interruptible(50);
  1957. if (signal_pending(current)) {
  1958. err = -EINTR;
  1959. goto error;
  1960. }
  1961. }
  1962. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
  1963. /* Get and check the revisions. */
  1964. fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
  1965. fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
  1966. fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
  1967. fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
  1968. if (fwrev <= 0x128) {
  1969. b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
  1970. "binary drivers older than version 4.x is unsupported. "
  1971. "You must upgrade your firmware files.\n");
  1972. b43_print_fw_helptext(dev->wl, 1);
  1973. err = -EOPNOTSUPP;
  1974. goto error;
  1975. }
  1976. dev->fw.rev = fwrev;
  1977. dev->fw.patch = fwpatch;
  1978. dev->fw.opensource = (fwdate == 0xFFFF);
  1979. if (dev->fw.opensource) {
  1980. /* Patchlevel info is encoded in the "time" field. */
  1981. dev->fw.patch = fwtime;
  1982. b43info(dev->wl, "Loading OpenSource firmware version %u.%u%s\n",
  1983. dev->fw.rev, dev->fw.patch,
  1984. dev->fw.pcm_request_failed ? " (Hardware crypto not supported)" : "");
  1985. } else {
  1986. b43info(dev->wl, "Loading firmware version %u.%u "
  1987. "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
  1988. fwrev, fwpatch,
  1989. (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
  1990. (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
  1991. if (dev->fw.pcm_request_failed) {
  1992. b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
  1993. "Hardware accelerated cryptography is disabled.\n");
  1994. b43_print_fw_helptext(dev->wl, 0);
  1995. }
  1996. }
  1997. if (b43_is_old_txhdr_format(dev)) {
  1998. b43warn(dev->wl, "You are using an old firmware image. "
  1999. "Support for old firmware will be removed in July 2008.\n");
  2000. b43_print_fw_helptext(dev->wl, 0);
  2001. }
  2002. return 0;
  2003. error:
  2004. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  2005. macctl &= ~B43_MACCTL_PSM_RUN;
  2006. macctl |= B43_MACCTL_PSM_JMP0;
  2007. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2008. return err;
  2009. }
  2010. static int b43_write_initvals(struct b43_wldev *dev,
  2011. const struct b43_iv *ivals,
  2012. size_t count,
  2013. size_t array_size)
  2014. {
  2015. const struct b43_iv *iv;
  2016. u16 offset;
  2017. size_t i;
  2018. bool bit32;
  2019. BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
  2020. iv = ivals;
  2021. for (i = 0; i < count; i++) {
  2022. if (array_size < sizeof(iv->offset_size))
  2023. goto err_format;
  2024. array_size -= sizeof(iv->offset_size);
  2025. offset = be16_to_cpu(iv->offset_size);
  2026. bit32 = !!(offset & B43_IV_32BIT);
  2027. offset &= B43_IV_OFFSET_MASK;
  2028. if (offset >= 0x1000)
  2029. goto err_format;
  2030. if (bit32) {
  2031. u32 value;
  2032. if (array_size < sizeof(iv->data.d32))
  2033. goto err_format;
  2034. array_size -= sizeof(iv->data.d32);
  2035. value = get_unaligned_be32(&iv->data.d32);
  2036. b43_write32(dev, offset, value);
  2037. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2038. sizeof(__be16) +
  2039. sizeof(__be32));
  2040. } else {
  2041. u16 value;
  2042. if (array_size < sizeof(iv->data.d16))
  2043. goto err_format;
  2044. array_size -= sizeof(iv->data.d16);
  2045. value = be16_to_cpu(iv->data.d16);
  2046. b43_write16(dev, offset, value);
  2047. iv = (const struct b43_iv *)((const uint8_t *)iv +
  2048. sizeof(__be16) +
  2049. sizeof(__be16));
  2050. }
  2051. }
  2052. if (array_size)
  2053. goto err_format;
  2054. return 0;
  2055. err_format:
  2056. b43err(dev->wl, "Initial Values Firmware file-format error.\n");
  2057. b43_print_fw_helptext(dev->wl, 1);
  2058. return -EPROTO;
  2059. }
  2060. static int b43_upload_initvals(struct b43_wldev *dev)
  2061. {
  2062. const size_t hdr_len = sizeof(struct b43_fw_header);
  2063. const struct b43_fw_header *hdr;
  2064. struct b43_firmware *fw = &dev->fw;
  2065. const struct b43_iv *ivals;
  2066. size_t count;
  2067. int err;
  2068. hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
  2069. ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
  2070. count = be32_to_cpu(hdr->size);
  2071. err = b43_write_initvals(dev, ivals, count,
  2072. fw->initvals.data->size - hdr_len);
  2073. if (err)
  2074. goto out;
  2075. if (fw->initvals_band.data) {
  2076. hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
  2077. ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
  2078. count = be32_to_cpu(hdr->size);
  2079. err = b43_write_initvals(dev, ivals, count,
  2080. fw->initvals_band.data->size - hdr_len);
  2081. if (err)
  2082. goto out;
  2083. }
  2084. out:
  2085. return err;
  2086. }
  2087. /* Initialize the GPIOs
  2088. * http://bcm-specs.sipsolutions.net/GPIO
  2089. */
  2090. static int b43_gpio_init(struct b43_wldev *dev)
  2091. {
  2092. struct ssb_bus *bus = dev->dev->bus;
  2093. struct ssb_device *gpiodev, *pcidev = NULL;
  2094. u32 mask, set;
  2095. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2096. & ~B43_MACCTL_GPOUTSMSK);
  2097. b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
  2098. | 0x000F);
  2099. mask = 0x0000001F;
  2100. set = 0x0000000F;
  2101. if (dev->dev->bus->chip_id == 0x4301) {
  2102. mask |= 0x0060;
  2103. set |= 0x0060;
  2104. }
  2105. if (0 /* FIXME: conditional unknown */ ) {
  2106. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2107. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2108. | 0x0100);
  2109. mask |= 0x0180;
  2110. set |= 0x0180;
  2111. }
  2112. if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
  2113. b43_write16(dev, B43_MMIO_GPIO_MASK,
  2114. b43_read16(dev, B43_MMIO_GPIO_MASK)
  2115. | 0x0200);
  2116. mask |= 0x0200;
  2117. set |= 0x0200;
  2118. }
  2119. if (dev->dev->id.revision >= 2)
  2120. mask |= 0x0010; /* FIXME: This is redundant. */
  2121. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2122. pcidev = bus->pcicore.dev;
  2123. #endif
  2124. gpiodev = bus->chipco.dev ? : pcidev;
  2125. if (!gpiodev)
  2126. return 0;
  2127. ssb_write32(gpiodev, B43_GPIO_CONTROL,
  2128. (ssb_read32(gpiodev, B43_GPIO_CONTROL)
  2129. & mask) | set);
  2130. return 0;
  2131. }
  2132. /* Turn off all GPIO stuff. Call this on module unload, for example. */
  2133. static void b43_gpio_cleanup(struct b43_wldev *dev)
  2134. {
  2135. struct ssb_bus *bus = dev->dev->bus;
  2136. struct ssb_device *gpiodev, *pcidev = NULL;
  2137. #ifdef CONFIG_SSB_DRIVER_PCICORE
  2138. pcidev = bus->pcicore.dev;
  2139. #endif
  2140. gpiodev = bus->chipco.dev ? : pcidev;
  2141. if (!gpiodev)
  2142. return;
  2143. ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
  2144. }
  2145. /* http://bcm-specs.sipsolutions.net/EnableMac */
  2146. void b43_mac_enable(struct b43_wldev *dev)
  2147. {
  2148. if (b43_debug(dev, B43_DBG_FIRMWARE)) {
  2149. u16 fwstate;
  2150. fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
  2151. B43_SHM_SH_UCODESTAT);
  2152. if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
  2153. (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
  2154. b43err(dev->wl, "b43_mac_enable(): The firmware "
  2155. "should be suspended, but current state is %u\n",
  2156. fwstate);
  2157. }
  2158. }
  2159. dev->mac_suspended--;
  2160. B43_WARN_ON(dev->mac_suspended < 0);
  2161. if (dev->mac_suspended == 0) {
  2162. b43_write32(dev, B43_MMIO_MACCTL,
  2163. b43_read32(dev, B43_MMIO_MACCTL)
  2164. | B43_MACCTL_ENABLED);
  2165. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
  2166. B43_IRQ_MAC_SUSPENDED);
  2167. /* Commit writes */
  2168. b43_read32(dev, B43_MMIO_MACCTL);
  2169. b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2170. b43_power_saving_ctl_bits(dev, 0);
  2171. }
  2172. }
  2173. /* http://bcm-specs.sipsolutions.net/SuspendMAC */
  2174. void b43_mac_suspend(struct b43_wldev *dev)
  2175. {
  2176. int i;
  2177. u32 tmp;
  2178. might_sleep();
  2179. B43_WARN_ON(dev->mac_suspended < 0);
  2180. if (dev->mac_suspended == 0) {
  2181. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  2182. b43_write32(dev, B43_MMIO_MACCTL,
  2183. b43_read32(dev, B43_MMIO_MACCTL)
  2184. & ~B43_MACCTL_ENABLED);
  2185. /* force pci to flush the write */
  2186. b43_read32(dev, B43_MMIO_MACCTL);
  2187. for (i = 35; i; i--) {
  2188. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2189. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2190. goto out;
  2191. udelay(10);
  2192. }
  2193. /* Hm, it seems this will take some time. Use msleep(). */
  2194. for (i = 40; i; i--) {
  2195. tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
  2196. if (tmp & B43_IRQ_MAC_SUSPENDED)
  2197. goto out;
  2198. msleep(1);
  2199. }
  2200. b43err(dev->wl, "MAC suspend failed\n");
  2201. }
  2202. out:
  2203. dev->mac_suspended++;
  2204. }
  2205. static void b43_adjust_opmode(struct b43_wldev *dev)
  2206. {
  2207. struct b43_wl *wl = dev->wl;
  2208. u32 ctl;
  2209. u16 cfp_pretbtt;
  2210. ctl = b43_read32(dev, B43_MMIO_MACCTL);
  2211. /* Reset status to STA infrastructure mode. */
  2212. ctl &= ~B43_MACCTL_AP;
  2213. ctl &= ~B43_MACCTL_KEEP_CTL;
  2214. ctl &= ~B43_MACCTL_KEEP_BADPLCP;
  2215. ctl &= ~B43_MACCTL_KEEP_BAD;
  2216. ctl &= ~B43_MACCTL_PROMISC;
  2217. ctl &= ~B43_MACCTL_BEACPROMISC;
  2218. ctl |= B43_MACCTL_INFRA;
  2219. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  2220. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  2221. ctl |= B43_MACCTL_AP;
  2222. else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
  2223. ctl &= ~B43_MACCTL_INFRA;
  2224. if (wl->filter_flags & FIF_CONTROL)
  2225. ctl |= B43_MACCTL_KEEP_CTL;
  2226. if (wl->filter_flags & FIF_FCSFAIL)
  2227. ctl |= B43_MACCTL_KEEP_BAD;
  2228. if (wl->filter_flags & FIF_PLCPFAIL)
  2229. ctl |= B43_MACCTL_KEEP_BADPLCP;
  2230. if (wl->filter_flags & FIF_PROMISC_IN_BSS)
  2231. ctl |= B43_MACCTL_PROMISC;
  2232. if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
  2233. ctl |= B43_MACCTL_BEACPROMISC;
  2234. /* Workaround: On old hardware the HW-MAC-address-filter
  2235. * doesn't work properly, so always run promisc in filter
  2236. * it in software. */
  2237. if (dev->dev->id.revision <= 4)
  2238. ctl |= B43_MACCTL_PROMISC;
  2239. b43_write32(dev, B43_MMIO_MACCTL, ctl);
  2240. cfp_pretbtt = 2;
  2241. if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
  2242. if (dev->dev->bus->chip_id == 0x4306 &&
  2243. dev->dev->bus->chip_rev == 3)
  2244. cfp_pretbtt = 100;
  2245. else
  2246. cfp_pretbtt = 50;
  2247. }
  2248. b43_write16(dev, 0x612, cfp_pretbtt);
  2249. }
  2250. static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
  2251. {
  2252. u16 offset;
  2253. if (is_ofdm) {
  2254. offset = 0x480;
  2255. offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
  2256. } else {
  2257. offset = 0x4C0;
  2258. offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
  2259. }
  2260. b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
  2261. b43_shm_read16(dev, B43_SHM_SHARED, offset));
  2262. }
  2263. static void b43_rate_memory_init(struct b43_wldev *dev)
  2264. {
  2265. switch (dev->phy.type) {
  2266. case B43_PHYTYPE_A:
  2267. case B43_PHYTYPE_G:
  2268. case B43_PHYTYPE_N:
  2269. b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
  2270. b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
  2271. b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
  2272. b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
  2273. b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
  2274. b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
  2275. b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
  2276. if (dev->phy.type == B43_PHYTYPE_A)
  2277. break;
  2278. /* fallthrough */
  2279. case B43_PHYTYPE_B:
  2280. b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
  2281. b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
  2282. b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
  2283. b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
  2284. break;
  2285. default:
  2286. B43_WARN_ON(1);
  2287. }
  2288. }
  2289. /* Set the default values for the PHY TX Control Words. */
  2290. static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
  2291. {
  2292. u16 ctl = 0;
  2293. ctl |= B43_TXH_PHY_ENC_CCK;
  2294. ctl |= B43_TXH_PHY_ANT01AUTO;
  2295. ctl |= B43_TXH_PHY_TXPWR;
  2296. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
  2297. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
  2298. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
  2299. }
  2300. /* Set the TX-Antenna for management frames sent by firmware. */
  2301. static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
  2302. {
  2303. u16 ant;
  2304. u16 tmp;
  2305. ant = b43_antenna_to_phyctl(antenna);
  2306. /* For ACK/CTS */
  2307. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
  2308. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2309. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
  2310. /* For Probe Resposes */
  2311. tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
  2312. tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
  2313. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
  2314. }
  2315. /* This is the opposite of b43_chip_init() */
  2316. static void b43_chip_exit(struct b43_wldev *dev)
  2317. {
  2318. b43_phy_exit(dev);
  2319. b43_gpio_cleanup(dev);
  2320. /* firmware is released later */
  2321. }
  2322. /* Initialize the chip
  2323. * http://bcm-specs.sipsolutions.net/ChipInit
  2324. */
  2325. static int b43_chip_init(struct b43_wldev *dev)
  2326. {
  2327. struct b43_phy *phy = &dev->phy;
  2328. int err;
  2329. u32 value32, macctl;
  2330. u16 value16;
  2331. /* Initialize the MAC control */
  2332. macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
  2333. if (dev->phy.gmode)
  2334. macctl |= B43_MACCTL_GMODE;
  2335. macctl |= B43_MACCTL_INFRA;
  2336. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  2337. err = b43_request_firmware(dev);
  2338. if (err)
  2339. goto out;
  2340. err = b43_upload_microcode(dev);
  2341. if (err)
  2342. goto out; /* firmware is released later */
  2343. err = b43_gpio_init(dev);
  2344. if (err)
  2345. goto out; /* firmware is released later */
  2346. err = b43_upload_initvals(dev);
  2347. if (err)
  2348. goto err_gpio_clean;
  2349. /* Turn the Analog on and initialize the PHY. */
  2350. phy->ops->switch_analog(dev, 1);
  2351. err = b43_phy_init(dev);
  2352. if (err)
  2353. goto err_gpio_clean;
  2354. /* Disable Interference Mitigation. */
  2355. if (phy->ops->interf_mitigation)
  2356. phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
  2357. /* Select the antennae */
  2358. if (phy->ops->set_rx_antenna)
  2359. phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
  2360. b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
  2361. if (phy->type == B43_PHYTYPE_B) {
  2362. value16 = b43_read16(dev, 0x005E);
  2363. value16 |= 0x0004;
  2364. b43_write16(dev, 0x005E, value16);
  2365. }
  2366. b43_write32(dev, 0x0100, 0x01000000);
  2367. if (dev->dev->id.revision < 5)
  2368. b43_write32(dev, 0x010C, 0x01000000);
  2369. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2370. & ~B43_MACCTL_INFRA);
  2371. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  2372. | B43_MACCTL_INFRA);
  2373. /* Probe Response Timeout value */
  2374. /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
  2375. b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
  2376. /* Initially set the wireless operation mode. */
  2377. b43_adjust_opmode(dev);
  2378. if (dev->dev->id.revision < 3) {
  2379. b43_write16(dev, 0x060E, 0x0000);
  2380. b43_write16(dev, 0x0610, 0x8000);
  2381. b43_write16(dev, 0x0604, 0x0000);
  2382. b43_write16(dev, 0x0606, 0x0200);
  2383. } else {
  2384. b43_write32(dev, 0x0188, 0x80000000);
  2385. b43_write32(dev, 0x018C, 0x02000000);
  2386. }
  2387. b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
  2388. b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
  2389. b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
  2390. b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
  2391. b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
  2392. b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
  2393. b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
  2394. value32 = ssb_read32(dev->dev, SSB_TMSLOW);
  2395. value32 |= 0x00100000;
  2396. ssb_write32(dev->dev, SSB_TMSLOW, value32);
  2397. b43_write16(dev, B43_MMIO_POWERUP_DELAY,
  2398. dev->dev->bus->chipco.fast_pwrup_delay);
  2399. err = 0;
  2400. b43dbg(dev->wl, "Chip initialized\n");
  2401. out:
  2402. return err;
  2403. err_gpio_clean:
  2404. b43_gpio_cleanup(dev);
  2405. return err;
  2406. }
  2407. static void b43_periodic_every60sec(struct b43_wldev *dev)
  2408. {
  2409. const struct b43_phy_operations *ops = dev->phy.ops;
  2410. if (ops->pwork_60sec)
  2411. ops->pwork_60sec(dev);
  2412. /* Force check the TX power emission now. */
  2413. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
  2414. }
  2415. static void b43_periodic_every30sec(struct b43_wldev *dev)
  2416. {
  2417. /* Update device statistics. */
  2418. b43_calculate_link_quality(dev);
  2419. }
  2420. static void b43_periodic_every15sec(struct b43_wldev *dev)
  2421. {
  2422. struct b43_phy *phy = &dev->phy;
  2423. u16 wdr;
  2424. if (dev->fw.opensource) {
  2425. /* Check if the firmware is still alive.
  2426. * It will reset the watchdog counter to 0 in its idle loop. */
  2427. wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
  2428. if (unlikely(wdr)) {
  2429. b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
  2430. b43_controller_restart(dev, "Firmware watchdog");
  2431. return;
  2432. } else {
  2433. b43_shm_write16(dev, B43_SHM_SCRATCH,
  2434. B43_WATCHDOG_REG, 1);
  2435. }
  2436. }
  2437. if (phy->ops->pwork_15sec)
  2438. phy->ops->pwork_15sec(dev);
  2439. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  2440. wmb();
  2441. }
  2442. static void do_periodic_work(struct b43_wldev *dev)
  2443. {
  2444. unsigned int state;
  2445. state = dev->periodic_state;
  2446. if (state % 4 == 0)
  2447. b43_periodic_every60sec(dev);
  2448. if (state % 2 == 0)
  2449. b43_periodic_every30sec(dev);
  2450. b43_periodic_every15sec(dev);
  2451. }
  2452. /* Periodic work locking policy:
  2453. * The whole periodic work handler is protected by
  2454. * wl->mutex. If another lock is needed somewhere in the
  2455. * pwork callchain, it's aquired in-place, where it's needed.
  2456. */
  2457. static void b43_periodic_work_handler(struct work_struct *work)
  2458. {
  2459. struct b43_wldev *dev = container_of(work, struct b43_wldev,
  2460. periodic_work.work);
  2461. struct b43_wl *wl = dev->wl;
  2462. unsigned long delay;
  2463. mutex_lock(&wl->mutex);
  2464. if (unlikely(b43_status(dev) != B43_STAT_STARTED))
  2465. goto out;
  2466. if (b43_debug(dev, B43_DBG_PWORK_STOP))
  2467. goto out_requeue;
  2468. do_periodic_work(dev);
  2469. dev->periodic_state++;
  2470. out_requeue:
  2471. if (b43_debug(dev, B43_DBG_PWORK_FAST))
  2472. delay = msecs_to_jiffies(50);
  2473. else
  2474. delay = round_jiffies_relative(HZ * 15);
  2475. queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
  2476. out:
  2477. mutex_unlock(&wl->mutex);
  2478. }
  2479. static void b43_periodic_tasks_setup(struct b43_wldev *dev)
  2480. {
  2481. struct delayed_work *work = &dev->periodic_work;
  2482. dev->periodic_state = 0;
  2483. INIT_DELAYED_WORK(work, b43_periodic_work_handler);
  2484. queue_delayed_work(dev->wl->hw->workqueue, work, 0);
  2485. }
  2486. /* Check if communication with the device works correctly. */
  2487. static int b43_validate_chipaccess(struct b43_wldev *dev)
  2488. {
  2489. u32 v, backup;
  2490. backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
  2491. /* Check for read/write and endianness problems. */
  2492. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
  2493. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
  2494. goto error;
  2495. b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
  2496. if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
  2497. goto error;
  2498. b43_shm_write32(dev, B43_SHM_SHARED, 0, backup);
  2499. if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
  2500. /* The 32bit register shadows the two 16bit registers
  2501. * with update sideeffects. Validate this. */
  2502. b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
  2503. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
  2504. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
  2505. goto error;
  2506. if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
  2507. goto error;
  2508. }
  2509. b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
  2510. v = b43_read32(dev, B43_MMIO_MACCTL);
  2511. v |= B43_MACCTL_GMODE;
  2512. if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
  2513. goto error;
  2514. return 0;
  2515. error:
  2516. b43err(dev->wl, "Failed to validate the chipaccess\n");
  2517. return -ENODEV;
  2518. }
  2519. static void b43_security_init(struct b43_wldev *dev)
  2520. {
  2521. dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
  2522. B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
  2523. dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
  2524. /* KTP is a word address, but we address SHM bytewise.
  2525. * So multiply by two.
  2526. */
  2527. dev->ktp *= 2;
  2528. if (dev->dev->id.revision >= 5) {
  2529. /* Number of RCMTA address slots */
  2530. b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
  2531. }
  2532. b43_clear_keys(dev);
  2533. }
  2534. static int b43_rng_read(struct hwrng *rng, u32 * data)
  2535. {
  2536. struct b43_wl *wl = (struct b43_wl *)rng->priv;
  2537. unsigned long flags;
  2538. /* Don't take wl->mutex here, as it could deadlock with
  2539. * hwrng internal locking. It's not needed to take
  2540. * wl->mutex here, anyway. */
  2541. spin_lock_irqsave(&wl->irq_lock, flags);
  2542. *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
  2543. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2544. return (sizeof(u16));
  2545. }
  2546. static void b43_rng_exit(struct b43_wl *wl)
  2547. {
  2548. if (wl->rng_initialized)
  2549. hwrng_unregister(&wl->rng);
  2550. }
  2551. static int b43_rng_init(struct b43_wl *wl)
  2552. {
  2553. int err;
  2554. snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
  2555. "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
  2556. wl->rng.name = wl->rng_name;
  2557. wl->rng.data_read = b43_rng_read;
  2558. wl->rng.priv = (unsigned long)wl;
  2559. wl->rng_initialized = 1;
  2560. err = hwrng_register(&wl->rng);
  2561. if (err) {
  2562. wl->rng_initialized = 0;
  2563. b43err(wl, "Failed to register the random "
  2564. "number generator (%d)\n", err);
  2565. }
  2566. return err;
  2567. }
  2568. static int b43_op_tx(struct ieee80211_hw *hw,
  2569. struct sk_buff *skb)
  2570. {
  2571. struct b43_wl *wl = hw_to_b43_wl(hw);
  2572. struct b43_wldev *dev = wl->current_dev;
  2573. unsigned long flags;
  2574. int err;
  2575. if (unlikely(skb->len < 2 + 2 + 6)) {
  2576. /* Too short, this can't be a valid frame. */
  2577. goto drop_packet;
  2578. }
  2579. B43_WARN_ON(skb_shinfo(skb)->nr_frags);
  2580. if (unlikely(!dev))
  2581. goto drop_packet;
  2582. /* Transmissions on seperate queues can run concurrently. */
  2583. read_lock_irqsave(&wl->tx_lock, flags);
  2584. err = -ENODEV;
  2585. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2586. if (b43_using_pio_transfers(dev))
  2587. err = b43_pio_tx(dev, skb);
  2588. else
  2589. err = b43_dma_tx(dev, skb);
  2590. }
  2591. read_unlock_irqrestore(&wl->tx_lock, flags);
  2592. if (unlikely(err))
  2593. goto drop_packet;
  2594. return NETDEV_TX_OK;
  2595. drop_packet:
  2596. /* We can not transmit this packet. Drop it. */
  2597. dev_kfree_skb_any(skb);
  2598. return NETDEV_TX_OK;
  2599. }
  2600. /* Locking: wl->irq_lock */
  2601. static void b43_qos_params_upload(struct b43_wldev *dev,
  2602. const struct ieee80211_tx_queue_params *p,
  2603. u16 shm_offset)
  2604. {
  2605. u16 params[B43_NR_QOSPARAMS];
  2606. int bslots, tmp;
  2607. unsigned int i;
  2608. bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
  2609. memset(&params, 0, sizeof(params));
  2610. params[B43_QOSPARAM_TXOP] = p->txop * 32;
  2611. params[B43_QOSPARAM_CWMIN] = p->cw_min;
  2612. params[B43_QOSPARAM_CWMAX] = p->cw_max;
  2613. params[B43_QOSPARAM_CWCUR] = p->cw_min;
  2614. params[B43_QOSPARAM_AIFS] = p->aifs;
  2615. params[B43_QOSPARAM_BSLOTS] = bslots;
  2616. params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
  2617. for (i = 0; i < ARRAY_SIZE(params); i++) {
  2618. if (i == B43_QOSPARAM_STATUS) {
  2619. tmp = b43_shm_read16(dev, B43_SHM_SHARED,
  2620. shm_offset + (i * 2));
  2621. /* Mark the parameters as updated. */
  2622. tmp |= 0x100;
  2623. b43_shm_write16(dev, B43_SHM_SHARED,
  2624. shm_offset + (i * 2),
  2625. tmp);
  2626. } else {
  2627. b43_shm_write16(dev, B43_SHM_SHARED,
  2628. shm_offset + (i * 2),
  2629. params[i]);
  2630. }
  2631. }
  2632. }
  2633. /* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
  2634. static const u16 b43_qos_shm_offsets[] = {
  2635. /* [mac80211-queue-nr] = SHM_OFFSET, */
  2636. [0] = B43_QOS_VOICE,
  2637. [1] = B43_QOS_VIDEO,
  2638. [2] = B43_QOS_BESTEFFORT,
  2639. [3] = B43_QOS_BACKGROUND,
  2640. };
  2641. /* Update all QOS parameters in hardware. */
  2642. static void b43_qos_upload_all(struct b43_wldev *dev)
  2643. {
  2644. struct b43_wl *wl = dev->wl;
  2645. struct b43_qos_params *params;
  2646. unsigned int i;
  2647. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2648. ARRAY_SIZE(wl->qos_params));
  2649. b43_mac_suspend(dev);
  2650. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2651. params = &(wl->qos_params[i]);
  2652. b43_qos_params_upload(dev, &(params->p),
  2653. b43_qos_shm_offsets[i]);
  2654. }
  2655. b43_mac_enable(dev);
  2656. }
  2657. static void b43_qos_clear(struct b43_wl *wl)
  2658. {
  2659. struct b43_qos_params *params;
  2660. unsigned int i;
  2661. /* Initialize QoS parameters to sane defaults. */
  2662. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2663. ARRAY_SIZE(wl->qos_params));
  2664. for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
  2665. params = &(wl->qos_params[i]);
  2666. switch (b43_qos_shm_offsets[i]) {
  2667. case B43_QOS_VOICE:
  2668. params->p.txop = 0;
  2669. params->p.aifs = 2;
  2670. params->p.cw_min = 0x0001;
  2671. params->p.cw_max = 0x0001;
  2672. break;
  2673. case B43_QOS_VIDEO:
  2674. params->p.txop = 0;
  2675. params->p.aifs = 2;
  2676. params->p.cw_min = 0x0001;
  2677. params->p.cw_max = 0x0001;
  2678. break;
  2679. case B43_QOS_BESTEFFORT:
  2680. params->p.txop = 0;
  2681. params->p.aifs = 3;
  2682. params->p.cw_min = 0x0001;
  2683. params->p.cw_max = 0x03FF;
  2684. break;
  2685. case B43_QOS_BACKGROUND:
  2686. params->p.txop = 0;
  2687. params->p.aifs = 7;
  2688. params->p.cw_min = 0x0001;
  2689. params->p.cw_max = 0x03FF;
  2690. break;
  2691. default:
  2692. B43_WARN_ON(1);
  2693. }
  2694. }
  2695. }
  2696. /* Initialize the core's QOS capabilities */
  2697. static void b43_qos_init(struct b43_wldev *dev)
  2698. {
  2699. /* Upload the current QOS parameters. */
  2700. b43_qos_upload_all(dev);
  2701. /* Enable QOS support. */
  2702. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
  2703. b43_write16(dev, B43_MMIO_IFSCTL,
  2704. b43_read16(dev, B43_MMIO_IFSCTL)
  2705. | B43_MMIO_IFSCTL_USE_EDCF);
  2706. }
  2707. static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
  2708. const struct ieee80211_tx_queue_params *params)
  2709. {
  2710. struct b43_wl *wl = hw_to_b43_wl(hw);
  2711. struct b43_wldev *dev;
  2712. unsigned int queue = (unsigned int)_queue;
  2713. int err = -ENODEV;
  2714. if (queue >= ARRAY_SIZE(wl->qos_params)) {
  2715. /* Queue not available or don't support setting
  2716. * params on this queue. Return success to not
  2717. * confuse mac80211. */
  2718. return 0;
  2719. }
  2720. BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
  2721. ARRAY_SIZE(wl->qos_params));
  2722. mutex_lock(&wl->mutex);
  2723. dev = wl->current_dev;
  2724. if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
  2725. goto out_unlock;
  2726. memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
  2727. b43_mac_suspend(dev);
  2728. b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
  2729. b43_qos_shm_offsets[queue]);
  2730. b43_mac_enable(dev);
  2731. err = 0;
  2732. out_unlock:
  2733. mutex_unlock(&wl->mutex);
  2734. return err;
  2735. }
  2736. static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
  2737. struct ieee80211_tx_queue_stats *stats)
  2738. {
  2739. struct b43_wl *wl = hw_to_b43_wl(hw);
  2740. struct b43_wldev *dev = wl->current_dev;
  2741. unsigned long flags;
  2742. int err = -ENODEV;
  2743. if (!dev)
  2744. goto out;
  2745. spin_lock_irqsave(&wl->irq_lock, flags);
  2746. if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
  2747. if (b43_using_pio_transfers(dev))
  2748. b43_pio_get_tx_stats(dev, stats);
  2749. else
  2750. b43_dma_get_tx_stats(dev, stats);
  2751. err = 0;
  2752. }
  2753. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2754. out:
  2755. return err;
  2756. }
  2757. static int b43_op_get_stats(struct ieee80211_hw *hw,
  2758. struct ieee80211_low_level_stats *stats)
  2759. {
  2760. struct b43_wl *wl = hw_to_b43_wl(hw);
  2761. unsigned long flags;
  2762. spin_lock_irqsave(&wl->irq_lock, flags);
  2763. memcpy(stats, &wl->ieee_stats, sizeof(*stats));
  2764. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2765. return 0;
  2766. }
  2767. static void b43_put_phy_into_reset(struct b43_wldev *dev)
  2768. {
  2769. struct ssb_device *sdev = dev->dev;
  2770. u32 tmslow;
  2771. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2772. tmslow &= ~B43_TMSLOW_GMODE;
  2773. tmslow |= B43_TMSLOW_PHYRESET;
  2774. tmslow |= SSB_TMSLOW_FGC;
  2775. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2776. msleep(1);
  2777. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  2778. tmslow &= ~SSB_TMSLOW_FGC;
  2779. tmslow |= B43_TMSLOW_PHYRESET;
  2780. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  2781. msleep(1);
  2782. }
  2783. static const char * band_to_string(enum ieee80211_band band)
  2784. {
  2785. switch (band) {
  2786. case IEEE80211_BAND_5GHZ:
  2787. return "5";
  2788. case IEEE80211_BAND_2GHZ:
  2789. return "2.4";
  2790. default:
  2791. break;
  2792. }
  2793. B43_WARN_ON(1);
  2794. return "";
  2795. }
  2796. /* Expects wl->mutex locked */
  2797. static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
  2798. {
  2799. struct b43_wldev *up_dev = NULL;
  2800. struct b43_wldev *down_dev;
  2801. struct b43_wldev *d;
  2802. int err;
  2803. bool gmode;
  2804. int prev_status;
  2805. /* Find a device and PHY which supports the band. */
  2806. list_for_each_entry(d, &wl->devlist, list) {
  2807. switch (chan->band) {
  2808. case IEEE80211_BAND_5GHZ:
  2809. if (d->phy.supports_5ghz) {
  2810. up_dev = d;
  2811. gmode = 0;
  2812. }
  2813. break;
  2814. case IEEE80211_BAND_2GHZ:
  2815. if (d->phy.supports_2ghz) {
  2816. up_dev = d;
  2817. gmode = 1;
  2818. }
  2819. break;
  2820. default:
  2821. B43_WARN_ON(1);
  2822. return -EINVAL;
  2823. }
  2824. if (up_dev)
  2825. break;
  2826. }
  2827. if (!up_dev) {
  2828. b43err(wl, "Could not find a device for %s-GHz band operation\n",
  2829. band_to_string(chan->band));
  2830. return -ENODEV;
  2831. }
  2832. if ((up_dev == wl->current_dev) &&
  2833. (!!wl->current_dev->phy.gmode == !!gmode)) {
  2834. /* This device is already running. */
  2835. return 0;
  2836. }
  2837. b43dbg(wl, "Switching to %s-GHz band\n",
  2838. band_to_string(chan->band));
  2839. down_dev = wl->current_dev;
  2840. prev_status = b43_status(down_dev);
  2841. /* Shutdown the currently running core. */
  2842. if (prev_status >= B43_STAT_STARTED)
  2843. b43_wireless_core_stop(down_dev);
  2844. if (prev_status >= B43_STAT_INITIALIZED)
  2845. b43_wireless_core_exit(down_dev);
  2846. if (down_dev != up_dev) {
  2847. /* We switch to a different core, so we put PHY into
  2848. * RESET on the old core. */
  2849. b43_put_phy_into_reset(down_dev);
  2850. }
  2851. /* Now start the new core. */
  2852. up_dev->phy.gmode = gmode;
  2853. if (prev_status >= B43_STAT_INITIALIZED) {
  2854. err = b43_wireless_core_init(up_dev);
  2855. if (err) {
  2856. b43err(wl, "Fatal: Could not initialize device for "
  2857. "selected %s-GHz band\n",
  2858. band_to_string(chan->band));
  2859. goto init_failure;
  2860. }
  2861. }
  2862. if (prev_status >= B43_STAT_STARTED) {
  2863. err = b43_wireless_core_start(up_dev);
  2864. if (err) {
  2865. b43err(wl, "Fatal: Coult not start device for "
  2866. "selected %s-GHz band\n",
  2867. band_to_string(chan->band));
  2868. b43_wireless_core_exit(up_dev);
  2869. goto init_failure;
  2870. }
  2871. }
  2872. B43_WARN_ON(b43_status(up_dev) != prev_status);
  2873. wl->current_dev = up_dev;
  2874. return 0;
  2875. init_failure:
  2876. /* Whoops, failed to init the new core. No core is operating now. */
  2877. wl->current_dev = NULL;
  2878. return err;
  2879. }
  2880. /* Write the short and long frame retry limit values. */
  2881. static void b43_set_retry_limits(struct b43_wldev *dev,
  2882. unsigned int short_retry,
  2883. unsigned int long_retry)
  2884. {
  2885. /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
  2886. * the chip-internal counter. */
  2887. short_retry = min(short_retry, (unsigned int)0xF);
  2888. long_retry = min(long_retry, (unsigned int)0xF);
  2889. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
  2890. short_retry);
  2891. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
  2892. long_retry);
  2893. }
  2894. static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
  2895. {
  2896. struct b43_wl *wl = hw_to_b43_wl(hw);
  2897. struct b43_wldev *dev;
  2898. struct b43_phy *phy;
  2899. struct ieee80211_conf *conf = &hw->conf;
  2900. unsigned long flags;
  2901. int antenna;
  2902. int err = 0;
  2903. u32 savedirqs;
  2904. mutex_lock(&wl->mutex);
  2905. /* Switch the band (if necessary). This might change the active core. */
  2906. err = b43_switch_band(wl, conf->channel);
  2907. if (err)
  2908. goto out_unlock_mutex;
  2909. dev = wl->current_dev;
  2910. phy = &dev->phy;
  2911. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  2912. b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
  2913. conf->long_frame_max_tx_count);
  2914. changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
  2915. if (!changed)
  2916. goto out_unlock_mutex;
  2917. /* Disable IRQs while reconfiguring the device.
  2918. * This makes it possible to drop the spinlock throughout
  2919. * the reconfiguration process. */
  2920. spin_lock_irqsave(&wl->irq_lock, flags);
  2921. if (b43_status(dev) < B43_STAT_STARTED) {
  2922. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2923. goto out_unlock_mutex;
  2924. }
  2925. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  2926. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2927. b43_synchronize_irq(dev);
  2928. /* Switch to the requested channel.
  2929. * The firmware takes care of races with the TX handler. */
  2930. if (conf->channel->hw_value != phy->channel)
  2931. b43_switch_channel(dev, conf->channel->hw_value);
  2932. dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
  2933. /* Adjust the desired TX power level. */
  2934. if (conf->power_level != 0) {
  2935. spin_lock_irqsave(&wl->irq_lock, flags);
  2936. if (conf->power_level != phy->desired_txpower) {
  2937. phy->desired_txpower = conf->power_level;
  2938. b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
  2939. B43_TXPWR_IGNORE_TSSI);
  2940. }
  2941. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2942. }
  2943. /* Antennas for RX and management frame TX. */
  2944. antenna = B43_ANTENNA_DEFAULT;
  2945. b43_mgmtframe_txantenna(dev, antenna);
  2946. antenna = B43_ANTENNA_DEFAULT;
  2947. if (phy->ops->set_rx_antenna)
  2948. phy->ops->set_rx_antenna(dev, antenna);
  2949. /* Update templates for AP/mesh mode. */
  2950. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  2951. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
  2952. b43_set_beacon_int(dev, conf->beacon_int);
  2953. if (!!conf->radio_enabled != phy->radio_on) {
  2954. if (conf->radio_enabled) {
  2955. b43_software_rfkill(dev, RFKILL_STATE_UNBLOCKED);
  2956. b43info(dev->wl, "Radio turned on by software\n");
  2957. if (!dev->radio_hw_enable) {
  2958. b43info(dev->wl, "The hardware RF-kill button "
  2959. "still turns the radio physically off. "
  2960. "Press the button to turn it on.\n");
  2961. }
  2962. } else {
  2963. b43_software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
  2964. b43info(dev->wl, "Radio turned off by software\n");
  2965. }
  2966. }
  2967. spin_lock_irqsave(&wl->irq_lock, flags);
  2968. b43_interrupt_enable(dev, savedirqs);
  2969. mmiowb();
  2970. spin_unlock_irqrestore(&wl->irq_lock, flags);
  2971. out_unlock_mutex:
  2972. mutex_unlock(&wl->mutex);
  2973. return err;
  2974. }
  2975. static void b43_update_basic_rates(struct b43_wldev *dev, u64 brates)
  2976. {
  2977. struct ieee80211_supported_band *sband =
  2978. dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
  2979. struct ieee80211_rate *rate;
  2980. int i;
  2981. u16 basic, direct, offset, basic_offset, rateptr;
  2982. for (i = 0; i < sband->n_bitrates; i++) {
  2983. rate = &sband->bitrates[i];
  2984. if (b43_is_cck_rate(rate->hw_value)) {
  2985. direct = B43_SHM_SH_CCKDIRECT;
  2986. basic = B43_SHM_SH_CCKBASIC;
  2987. offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  2988. offset &= 0xF;
  2989. } else {
  2990. direct = B43_SHM_SH_OFDMDIRECT;
  2991. basic = B43_SHM_SH_OFDMBASIC;
  2992. offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  2993. offset &= 0xF;
  2994. }
  2995. rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
  2996. if (b43_is_cck_rate(rate->hw_value)) {
  2997. basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
  2998. basic_offset &= 0xF;
  2999. } else {
  3000. basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
  3001. basic_offset &= 0xF;
  3002. }
  3003. /*
  3004. * Get the pointer that we need to point to
  3005. * from the direct map
  3006. */
  3007. rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
  3008. direct + 2 * basic_offset);
  3009. /* and write it to the basic map */
  3010. b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
  3011. rateptr);
  3012. }
  3013. }
  3014. static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
  3015. struct ieee80211_vif *vif,
  3016. struct ieee80211_bss_conf *conf,
  3017. u32 changed)
  3018. {
  3019. struct b43_wl *wl = hw_to_b43_wl(hw);
  3020. struct b43_wldev *dev;
  3021. struct b43_phy *phy;
  3022. unsigned long flags;
  3023. u32 savedirqs;
  3024. mutex_lock(&wl->mutex);
  3025. dev = wl->current_dev;
  3026. phy = &dev->phy;
  3027. /* Disable IRQs while reconfiguring the device.
  3028. * This makes it possible to drop the spinlock throughout
  3029. * the reconfiguration process. */
  3030. spin_lock_irqsave(&wl->irq_lock, flags);
  3031. if (b43_status(dev) < B43_STAT_STARTED) {
  3032. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3033. goto out_unlock_mutex;
  3034. }
  3035. savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
  3036. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3037. b43_synchronize_irq(dev);
  3038. b43_mac_suspend(dev);
  3039. if (changed & BSS_CHANGED_BASIC_RATES)
  3040. b43_update_basic_rates(dev, conf->basic_rates);
  3041. if (changed & BSS_CHANGED_ERP_SLOT) {
  3042. if (conf->use_short_slot)
  3043. b43_short_slot_timing_enable(dev);
  3044. else
  3045. b43_short_slot_timing_disable(dev);
  3046. }
  3047. b43_mac_enable(dev);
  3048. spin_lock_irqsave(&wl->irq_lock, flags);
  3049. b43_interrupt_enable(dev, savedirqs);
  3050. /* XXX: why? */
  3051. mmiowb();
  3052. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3053. out_unlock_mutex:
  3054. mutex_unlock(&wl->mutex);
  3055. return;
  3056. }
  3057. static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3058. const u8 *local_addr, const u8 *addr,
  3059. struct ieee80211_key_conf *key)
  3060. {
  3061. struct b43_wl *wl = hw_to_b43_wl(hw);
  3062. struct b43_wldev *dev;
  3063. unsigned long flags;
  3064. u8 algorithm;
  3065. u8 index;
  3066. int err;
  3067. if (modparam_nohwcrypt)
  3068. return -ENOSPC; /* User disabled HW-crypto */
  3069. mutex_lock(&wl->mutex);
  3070. spin_lock_irqsave(&wl->irq_lock, flags);
  3071. dev = wl->current_dev;
  3072. err = -ENODEV;
  3073. if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
  3074. goto out_unlock;
  3075. if (dev->fw.pcm_request_failed) {
  3076. /* We don't have firmware for the crypto engine.
  3077. * Must use software-crypto. */
  3078. err = -EOPNOTSUPP;
  3079. goto out_unlock;
  3080. }
  3081. err = -EINVAL;
  3082. switch (key->alg) {
  3083. case ALG_WEP:
  3084. if (key->keylen == 5)
  3085. algorithm = B43_SEC_ALGO_WEP40;
  3086. else
  3087. algorithm = B43_SEC_ALGO_WEP104;
  3088. break;
  3089. case ALG_TKIP:
  3090. algorithm = B43_SEC_ALGO_TKIP;
  3091. break;
  3092. case ALG_CCMP:
  3093. algorithm = B43_SEC_ALGO_AES;
  3094. break;
  3095. default:
  3096. B43_WARN_ON(1);
  3097. goto out_unlock;
  3098. }
  3099. index = (u8) (key->keyidx);
  3100. if (index > 3)
  3101. goto out_unlock;
  3102. switch (cmd) {
  3103. case SET_KEY:
  3104. if (algorithm == B43_SEC_ALGO_TKIP) {
  3105. /* FIXME: No TKIP hardware encryption for now. */
  3106. err = -EOPNOTSUPP;
  3107. goto out_unlock;
  3108. }
  3109. if (is_broadcast_ether_addr(addr)) {
  3110. /* addr is FF:FF:FF:FF:FF:FF for default keys */
  3111. err = b43_key_write(dev, index, algorithm,
  3112. key->key, key->keylen, NULL, key);
  3113. } else {
  3114. /*
  3115. * either pairwise key or address is 00:00:00:00:00:00
  3116. * for transmit-only keys
  3117. */
  3118. err = b43_key_write(dev, -1, algorithm,
  3119. key->key, key->keylen, addr, key);
  3120. }
  3121. if (err)
  3122. goto out_unlock;
  3123. if (algorithm == B43_SEC_ALGO_WEP40 ||
  3124. algorithm == B43_SEC_ALGO_WEP104) {
  3125. b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
  3126. } else {
  3127. b43_hf_write(dev,
  3128. b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
  3129. }
  3130. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  3131. break;
  3132. case DISABLE_KEY: {
  3133. err = b43_key_clear(dev, key->hw_key_idx);
  3134. if (err)
  3135. goto out_unlock;
  3136. break;
  3137. }
  3138. default:
  3139. B43_WARN_ON(1);
  3140. }
  3141. out_unlock:
  3142. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3143. mutex_unlock(&wl->mutex);
  3144. if (!err) {
  3145. b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
  3146. "mac: %pM\n",
  3147. cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
  3148. addr);
  3149. }
  3150. return err;
  3151. }
  3152. static void b43_op_configure_filter(struct ieee80211_hw *hw,
  3153. unsigned int changed, unsigned int *fflags,
  3154. int mc_count, struct dev_addr_list *mc_list)
  3155. {
  3156. struct b43_wl *wl = hw_to_b43_wl(hw);
  3157. struct b43_wldev *dev = wl->current_dev;
  3158. unsigned long flags;
  3159. if (!dev) {
  3160. *fflags = 0;
  3161. return;
  3162. }
  3163. spin_lock_irqsave(&wl->irq_lock, flags);
  3164. *fflags &= FIF_PROMISC_IN_BSS |
  3165. FIF_ALLMULTI |
  3166. FIF_FCSFAIL |
  3167. FIF_PLCPFAIL |
  3168. FIF_CONTROL |
  3169. FIF_OTHER_BSS |
  3170. FIF_BCN_PRBRESP_PROMISC;
  3171. changed &= FIF_PROMISC_IN_BSS |
  3172. FIF_ALLMULTI |
  3173. FIF_FCSFAIL |
  3174. FIF_PLCPFAIL |
  3175. FIF_CONTROL |
  3176. FIF_OTHER_BSS |
  3177. FIF_BCN_PRBRESP_PROMISC;
  3178. wl->filter_flags = *fflags;
  3179. if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
  3180. b43_adjust_opmode(dev);
  3181. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3182. }
  3183. static int b43_op_config_interface(struct ieee80211_hw *hw,
  3184. struct ieee80211_vif *vif,
  3185. struct ieee80211_if_conf *conf)
  3186. {
  3187. struct b43_wl *wl = hw_to_b43_wl(hw);
  3188. struct b43_wldev *dev = wl->current_dev;
  3189. unsigned long flags;
  3190. if (!dev)
  3191. return -ENODEV;
  3192. mutex_lock(&wl->mutex);
  3193. spin_lock_irqsave(&wl->irq_lock, flags);
  3194. B43_WARN_ON(wl->vif != vif);
  3195. if (conf->bssid)
  3196. memcpy(wl->bssid, conf->bssid, ETH_ALEN);
  3197. else
  3198. memset(wl->bssid, 0, ETH_ALEN);
  3199. if (b43_status(dev) >= B43_STAT_INITIALIZED) {
  3200. if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
  3201. b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT)) {
  3202. B43_WARN_ON(vif->type != wl->if_type);
  3203. if (conf->changed & IEEE80211_IFCC_BEACON)
  3204. b43_update_templates(wl);
  3205. } else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
  3206. if (conf->changed & IEEE80211_IFCC_BEACON)
  3207. b43_update_templates(wl);
  3208. }
  3209. b43_write_mac_bssid_templates(dev);
  3210. }
  3211. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3212. mutex_unlock(&wl->mutex);
  3213. return 0;
  3214. }
  3215. /* Locking: wl->mutex */
  3216. static void b43_wireless_core_stop(struct b43_wldev *dev)
  3217. {
  3218. struct b43_wl *wl = dev->wl;
  3219. unsigned long flags;
  3220. if (b43_status(dev) < B43_STAT_STARTED)
  3221. return;
  3222. /* Disable and sync interrupts. We must do this before than
  3223. * setting the status to INITIALIZED, as the interrupt handler
  3224. * won't care about IRQs then. */
  3225. spin_lock_irqsave(&wl->irq_lock, flags);
  3226. dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
  3227. b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
  3228. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3229. b43_synchronize_irq(dev);
  3230. write_lock_irqsave(&wl->tx_lock, flags);
  3231. b43_set_status(dev, B43_STAT_INITIALIZED);
  3232. write_unlock_irqrestore(&wl->tx_lock, flags);
  3233. b43_pio_stop(dev);
  3234. mutex_unlock(&wl->mutex);
  3235. /* Must unlock as it would otherwise deadlock. No races here.
  3236. * Cancel the possibly running self-rearming periodic work. */
  3237. cancel_delayed_work_sync(&dev->periodic_work);
  3238. mutex_lock(&wl->mutex);
  3239. b43_mac_suspend(dev);
  3240. free_irq(dev->dev->irq, dev);
  3241. b43dbg(wl, "Wireless interface stopped\n");
  3242. }
  3243. /* Locking: wl->mutex */
  3244. static int b43_wireless_core_start(struct b43_wldev *dev)
  3245. {
  3246. int err;
  3247. B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
  3248. drain_txstatus_queue(dev);
  3249. err = request_irq(dev->dev->irq, b43_interrupt_handler,
  3250. IRQF_SHARED, KBUILD_MODNAME, dev);
  3251. if (err) {
  3252. b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
  3253. goto out;
  3254. }
  3255. /* We are ready to run. */
  3256. b43_set_status(dev, B43_STAT_STARTED);
  3257. /* Start data flow (TX/RX). */
  3258. b43_mac_enable(dev);
  3259. b43_interrupt_enable(dev, dev->irq_savedstate);
  3260. /* Start maintainance work */
  3261. b43_periodic_tasks_setup(dev);
  3262. b43dbg(dev->wl, "Wireless interface started\n");
  3263. out:
  3264. return err;
  3265. }
  3266. /* Get PHY and RADIO versioning numbers */
  3267. static int b43_phy_versioning(struct b43_wldev *dev)
  3268. {
  3269. struct b43_phy *phy = &dev->phy;
  3270. u32 tmp;
  3271. u8 analog_type;
  3272. u8 phy_type;
  3273. u8 phy_rev;
  3274. u16 radio_manuf;
  3275. u16 radio_ver;
  3276. u16 radio_rev;
  3277. int unsupported = 0;
  3278. /* Get PHY versioning */
  3279. tmp = b43_read16(dev, B43_MMIO_PHY_VER);
  3280. analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
  3281. phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
  3282. phy_rev = (tmp & B43_PHYVER_VERSION);
  3283. switch (phy_type) {
  3284. case B43_PHYTYPE_A:
  3285. if (phy_rev >= 4)
  3286. unsupported = 1;
  3287. break;
  3288. case B43_PHYTYPE_B:
  3289. if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
  3290. && phy_rev != 7)
  3291. unsupported = 1;
  3292. break;
  3293. case B43_PHYTYPE_G:
  3294. if (phy_rev > 9)
  3295. unsupported = 1;
  3296. break;
  3297. #ifdef CONFIG_B43_NPHY
  3298. case B43_PHYTYPE_N:
  3299. if (phy_rev > 1)
  3300. unsupported = 1;
  3301. break;
  3302. #endif
  3303. default:
  3304. unsupported = 1;
  3305. };
  3306. if (unsupported) {
  3307. b43err(dev->wl, "FOUND UNSUPPORTED PHY "
  3308. "(Analog %u, Type %u, Revision %u)\n",
  3309. analog_type, phy_type, phy_rev);
  3310. return -EOPNOTSUPP;
  3311. }
  3312. b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
  3313. analog_type, phy_type, phy_rev);
  3314. /* Get RADIO versioning */
  3315. if (dev->dev->bus->chip_id == 0x4317) {
  3316. if (dev->dev->bus->chip_rev == 0)
  3317. tmp = 0x3205017F;
  3318. else if (dev->dev->bus->chip_rev == 1)
  3319. tmp = 0x4205017F;
  3320. else
  3321. tmp = 0x5205017F;
  3322. } else {
  3323. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3324. tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
  3325. b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
  3326. tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
  3327. }
  3328. radio_manuf = (tmp & 0x00000FFF);
  3329. radio_ver = (tmp & 0x0FFFF000) >> 12;
  3330. radio_rev = (tmp & 0xF0000000) >> 28;
  3331. if (radio_manuf != 0x17F /* Broadcom */)
  3332. unsupported = 1;
  3333. switch (phy_type) {
  3334. case B43_PHYTYPE_A:
  3335. if (radio_ver != 0x2060)
  3336. unsupported = 1;
  3337. if (radio_rev != 1)
  3338. unsupported = 1;
  3339. if (radio_manuf != 0x17F)
  3340. unsupported = 1;
  3341. break;
  3342. case B43_PHYTYPE_B:
  3343. if ((radio_ver & 0xFFF0) != 0x2050)
  3344. unsupported = 1;
  3345. break;
  3346. case B43_PHYTYPE_G:
  3347. if (radio_ver != 0x2050)
  3348. unsupported = 1;
  3349. break;
  3350. case B43_PHYTYPE_N:
  3351. if (radio_ver != 0x2055)
  3352. unsupported = 1;
  3353. break;
  3354. default:
  3355. B43_WARN_ON(1);
  3356. }
  3357. if (unsupported) {
  3358. b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
  3359. "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
  3360. radio_manuf, radio_ver, radio_rev);
  3361. return -EOPNOTSUPP;
  3362. }
  3363. b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
  3364. radio_manuf, radio_ver, radio_rev);
  3365. phy->radio_manuf = radio_manuf;
  3366. phy->radio_ver = radio_ver;
  3367. phy->radio_rev = radio_rev;
  3368. phy->analog = analog_type;
  3369. phy->type = phy_type;
  3370. phy->rev = phy_rev;
  3371. return 0;
  3372. }
  3373. static void setup_struct_phy_for_init(struct b43_wldev *dev,
  3374. struct b43_phy *phy)
  3375. {
  3376. phy->hardware_power_control = !!modparam_hwpctl;
  3377. phy->next_txpwr_check_time = jiffies;
  3378. /* PHY TX errors counter. */
  3379. atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
  3380. }
  3381. static void setup_struct_wldev_for_init(struct b43_wldev *dev)
  3382. {
  3383. dev->dfq_valid = 0;
  3384. /* Assume the radio is enabled. If it's not enabled, the state will
  3385. * immediately get fixed on the first periodic work run. */
  3386. dev->radio_hw_enable = 1;
  3387. /* Stats */
  3388. memset(&dev->stats, 0, sizeof(dev->stats));
  3389. setup_struct_phy_for_init(dev, &dev->phy);
  3390. /* IRQ related flags */
  3391. dev->irq_reason = 0;
  3392. memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
  3393. dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
  3394. dev->mac_suspended = 1;
  3395. /* Noise calculation context */
  3396. memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
  3397. }
  3398. static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
  3399. {
  3400. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  3401. u64 hf;
  3402. if (!modparam_btcoex)
  3403. return;
  3404. if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
  3405. return;
  3406. if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
  3407. return;
  3408. hf = b43_hf_read(dev);
  3409. if (sprom->boardflags_lo & B43_BFL_BTCMOD)
  3410. hf |= B43_HF_BTCOEXALT;
  3411. else
  3412. hf |= B43_HF_BTCOEX;
  3413. b43_hf_write(dev, hf);
  3414. }
  3415. static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
  3416. {
  3417. if (!modparam_btcoex)
  3418. return;
  3419. //TODO
  3420. }
  3421. static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
  3422. {
  3423. #ifdef CONFIG_SSB_DRIVER_PCICORE
  3424. struct ssb_bus *bus = dev->dev->bus;
  3425. u32 tmp;
  3426. if (bus->pcicore.dev &&
  3427. bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
  3428. bus->pcicore.dev->id.revision <= 5) {
  3429. /* IMCFGLO timeouts workaround. */
  3430. tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
  3431. tmp &= ~SSB_IMCFGLO_REQTO;
  3432. tmp &= ~SSB_IMCFGLO_SERTO;
  3433. switch (bus->bustype) {
  3434. case SSB_BUSTYPE_PCI:
  3435. case SSB_BUSTYPE_PCMCIA:
  3436. tmp |= 0x32;
  3437. break;
  3438. case SSB_BUSTYPE_SSB:
  3439. tmp |= 0x53;
  3440. break;
  3441. }
  3442. ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
  3443. }
  3444. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  3445. }
  3446. static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
  3447. {
  3448. u16 pu_delay;
  3449. /* The time value is in microseconds. */
  3450. if (dev->phy.type == B43_PHYTYPE_A)
  3451. pu_delay = 3700;
  3452. else
  3453. pu_delay = 1050;
  3454. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
  3455. pu_delay = 500;
  3456. if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
  3457. pu_delay = max(pu_delay, (u16)2400);
  3458. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
  3459. }
  3460. /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
  3461. static void b43_set_pretbtt(struct b43_wldev *dev)
  3462. {
  3463. u16 pretbtt;
  3464. /* The time value is in microseconds. */
  3465. if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
  3466. pretbtt = 2;
  3467. } else {
  3468. if (dev->phy.type == B43_PHYTYPE_A)
  3469. pretbtt = 120;
  3470. else
  3471. pretbtt = 250;
  3472. }
  3473. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
  3474. b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
  3475. }
  3476. /* Shutdown a wireless core */
  3477. /* Locking: wl->mutex */
  3478. static void b43_wireless_core_exit(struct b43_wldev *dev)
  3479. {
  3480. u32 macctl;
  3481. B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
  3482. if (b43_status(dev) != B43_STAT_INITIALIZED)
  3483. return;
  3484. b43_set_status(dev, B43_STAT_UNINIT);
  3485. /* Stop the microcode PSM. */
  3486. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  3487. macctl &= ~B43_MACCTL_PSM_RUN;
  3488. macctl |= B43_MACCTL_PSM_JMP0;
  3489. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  3490. if (!dev->suspend_in_progress) {
  3491. b43_leds_exit(dev);
  3492. b43_rng_exit(dev->wl);
  3493. }
  3494. b43_dma_free(dev);
  3495. b43_pio_free(dev);
  3496. b43_chip_exit(dev);
  3497. dev->phy.ops->switch_analog(dev, 0);
  3498. if (dev->wl->current_beacon) {
  3499. dev_kfree_skb_any(dev->wl->current_beacon);
  3500. dev->wl->current_beacon = NULL;
  3501. }
  3502. ssb_device_disable(dev->dev, 0);
  3503. ssb_bus_may_powerdown(dev->dev->bus);
  3504. }
  3505. /* Initialize a wireless core */
  3506. static int b43_wireless_core_init(struct b43_wldev *dev)
  3507. {
  3508. struct b43_wl *wl = dev->wl;
  3509. struct ssb_bus *bus = dev->dev->bus;
  3510. struct ssb_sprom *sprom = &bus->sprom;
  3511. struct b43_phy *phy = &dev->phy;
  3512. int err;
  3513. u64 hf;
  3514. u32 tmp;
  3515. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3516. err = ssb_bus_powerup(bus, 0);
  3517. if (err)
  3518. goto out;
  3519. if (!ssb_device_is_enabled(dev->dev)) {
  3520. tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
  3521. b43_wireless_core_reset(dev, tmp);
  3522. }
  3523. /* Reset all data structures. */
  3524. setup_struct_wldev_for_init(dev);
  3525. phy->ops->prepare_structs(dev);
  3526. /* Enable IRQ routing to this device. */
  3527. ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
  3528. b43_imcfglo_timeouts_workaround(dev);
  3529. b43_bluetooth_coext_disable(dev);
  3530. if (phy->ops->prepare_hardware) {
  3531. err = phy->ops->prepare_hardware(dev);
  3532. if (err)
  3533. goto err_busdown;
  3534. }
  3535. err = b43_chip_init(dev);
  3536. if (err)
  3537. goto err_busdown;
  3538. b43_shm_write16(dev, B43_SHM_SHARED,
  3539. B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
  3540. hf = b43_hf_read(dev);
  3541. if (phy->type == B43_PHYTYPE_G) {
  3542. hf |= B43_HF_SYMW;
  3543. if (phy->rev == 1)
  3544. hf |= B43_HF_GDCW;
  3545. if (sprom->boardflags_lo & B43_BFL_PACTRL)
  3546. hf |= B43_HF_OFDMPABOOST;
  3547. } else if (phy->type == B43_PHYTYPE_B) {
  3548. hf |= B43_HF_SYMW;
  3549. if (phy->rev >= 2 && phy->radio_ver == 0x2050)
  3550. hf &= ~B43_HF_GDCW;
  3551. }
  3552. b43_hf_write(dev, hf);
  3553. b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
  3554. B43_DEFAULT_LONG_RETRY_LIMIT);
  3555. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
  3556. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
  3557. /* Disable sending probe responses from firmware.
  3558. * Setting the MaxTime to one usec will always trigger
  3559. * a timeout, so we never send any probe resp.
  3560. * A timeout of zero is infinite. */
  3561. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
  3562. b43_rate_memory_init(dev);
  3563. b43_set_phytxctl_defaults(dev);
  3564. /* Minimum Contention Window */
  3565. if (phy->type == B43_PHYTYPE_B) {
  3566. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
  3567. } else {
  3568. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
  3569. }
  3570. /* Maximum Contention Window */
  3571. b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
  3572. if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
  3573. dev->__using_pio_transfers = 1;
  3574. err = b43_pio_init(dev);
  3575. } else {
  3576. dev->__using_pio_transfers = 0;
  3577. err = b43_dma_init(dev);
  3578. }
  3579. if (err)
  3580. goto err_chip_exit;
  3581. b43_qos_init(dev);
  3582. b43_set_synth_pu_delay(dev, 1);
  3583. b43_bluetooth_coext_enable(dev);
  3584. ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
  3585. b43_upload_card_macaddress(dev);
  3586. b43_security_init(dev);
  3587. if (!dev->suspend_in_progress)
  3588. b43_rng_init(wl);
  3589. b43_set_status(dev, B43_STAT_INITIALIZED);
  3590. if (!dev->suspend_in_progress)
  3591. b43_leds_init(dev);
  3592. out:
  3593. return err;
  3594. err_chip_exit:
  3595. b43_chip_exit(dev);
  3596. err_busdown:
  3597. ssb_bus_may_powerdown(bus);
  3598. B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
  3599. return err;
  3600. }
  3601. static int b43_op_add_interface(struct ieee80211_hw *hw,
  3602. struct ieee80211_if_init_conf *conf)
  3603. {
  3604. struct b43_wl *wl = hw_to_b43_wl(hw);
  3605. struct b43_wldev *dev;
  3606. unsigned long flags;
  3607. int err = -EOPNOTSUPP;
  3608. /* TODO: allow WDS/AP devices to coexist */
  3609. if (conf->type != NL80211_IFTYPE_AP &&
  3610. conf->type != NL80211_IFTYPE_MESH_POINT &&
  3611. conf->type != NL80211_IFTYPE_STATION &&
  3612. conf->type != NL80211_IFTYPE_WDS &&
  3613. conf->type != NL80211_IFTYPE_ADHOC)
  3614. return -EOPNOTSUPP;
  3615. mutex_lock(&wl->mutex);
  3616. if (wl->operating)
  3617. goto out_mutex_unlock;
  3618. b43dbg(wl, "Adding Interface type %d\n", conf->type);
  3619. dev = wl->current_dev;
  3620. wl->operating = 1;
  3621. wl->vif = conf->vif;
  3622. wl->if_type = conf->type;
  3623. memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
  3624. spin_lock_irqsave(&wl->irq_lock, flags);
  3625. b43_adjust_opmode(dev);
  3626. b43_set_pretbtt(dev);
  3627. b43_set_synth_pu_delay(dev, 0);
  3628. b43_upload_card_macaddress(dev);
  3629. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3630. err = 0;
  3631. out_mutex_unlock:
  3632. mutex_unlock(&wl->mutex);
  3633. return err;
  3634. }
  3635. static void b43_op_remove_interface(struct ieee80211_hw *hw,
  3636. struct ieee80211_if_init_conf *conf)
  3637. {
  3638. struct b43_wl *wl = hw_to_b43_wl(hw);
  3639. struct b43_wldev *dev = wl->current_dev;
  3640. unsigned long flags;
  3641. b43dbg(wl, "Removing Interface type %d\n", conf->type);
  3642. mutex_lock(&wl->mutex);
  3643. B43_WARN_ON(!wl->operating);
  3644. B43_WARN_ON(wl->vif != conf->vif);
  3645. wl->vif = NULL;
  3646. wl->operating = 0;
  3647. spin_lock_irqsave(&wl->irq_lock, flags);
  3648. b43_adjust_opmode(dev);
  3649. memset(wl->mac_addr, 0, ETH_ALEN);
  3650. b43_upload_card_macaddress(dev);
  3651. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3652. mutex_unlock(&wl->mutex);
  3653. }
  3654. static int b43_op_start(struct ieee80211_hw *hw)
  3655. {
  3656. struct b43_wl *wl = hw_to_b43_wl(hw);
  3657. struct b43_wldev *dev = wl->current_dev;
  3658. int did_init = 0;
  3659. int err = 0;
  3660. bool do_rfkill_exit = 0;
  3661. /* Kill all old instance specific information to make sure
  3662. * the card won't use it in the short timeframe between start
  3663. * and mac80211 reconfiguring it. */
  3664. memset(wl->bssid, 0, ETH_ALEN);
  3665. memset(wl->mac_addr, 0, ETH_ALEN);
  3666. wl->filter_flags = 0;
  3667. wl->radiotap_enabled = 0;
  3668. b43_qos_clear(wl);
  3669. wl->beacon0_uploaded = 0;
  3670. wl->beacon1_uploaded = 0;
  3671. wl->beacon_templates_virgin = 1;
  3672. /* First register RFkill.
  3673. * LEDs that are registered later depend on it. */
  3674. b43_rfkill_init(dev);
  3675. mutex_lock(&wl->mutex);
  3676. if (b43_status(dev) < B43_STAT_INITIALIZED) {
  3677. err = b43_wireless_core_init(dev);
  3678. if (err) {
  3679. do_rfkill_exit = 1;
  3680. goto out_mutex_unlock;
  3681. }
  3682. did_init = 1;
  3683. }
  3684. if (b43_status(dev) < B43_STAT_STARTED) {
  3685. err = b43_wireless_core_start(dev);
  3686. if (err) {
  3687. if (did_init)
  3688. b43_wireless_core_exit(dev);
  3689. do_rfkill_exit = 1;
  3690. goto out_mutex_unlock;
  3691. }
  3692. }
  3693. out_mutex_unlock:
  3694. mutex_unlock(&wl->mutex);
  3695. if (do_rfkill_exit)
  3696. b43_rfkill_exit(dev);
  3697. return err;
  3698. }
  3699. static void b43_op_stop(struct ieee80211_hw *hw)
  3700. {
  3701. struct b43_wl *wl = hw_to_b43_wl(hw);
  3702. struct b43_wldev *dev = wl->current_dev;
  3703. b43_rfkill_exit(dev);
  3704. cancel_work_sync(&(wl->beacon_update_trigger));
  3705. mutex_lock(&wl->mutex);
  3706. if (b43_status(dev) >= B43_STAT_STARTED)
  3707. b43_wireless_core_stop(dev);
  3708. b43_wireless_core_exit(dev);
  3709. mutex_unlock(&wl->mutex);
  3710. cancel_work_sync(&(wl->txpower_adjust_work));
  3711. }
  3712. static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
  3713. struct ieee80211_sta *sta, bool set)
  3714. {
  3715. struct b43_wl *wl = hw_to_b43_wl(hw);
  3716. unsigned long flags;
  3717. spin_lock_irqsave(&wl->irq_lock, flags);
  3718. b43_update_templates(wl);
  3719. spin_unlock_irqrestore(&wl->irq_lock, flags);
  3720. return 0;
  3721. }
  3722. static void b43_op_sta_notify(struct ieee80211_hw *hw,
  3723. struct ieee80211_vif *vif,
  3724. enum sta_notify_cmd notify_cmd,
  3725. struct ieee80211_sta *sta)
  3726. {
  3727. struct b43_wl *wl = hw_to_b43_wl(hw);
  3728. B43_WARN_ON(!vif || wl->vif != vif);
  3729. }
  3730. static const struct ieee80211_ops b43_hw_ops = {
  3731. .tx = b43_op_tx,
  3732. .conf_tx = b43_op_conf_tx,
  3733. .add_interface = b43_op_add_interface,
  3734. .remove_interface = b43_op_remove_interface,
  3735. .config = b43_op_config,
  3736. .bss_info_changed = b43_op_bss_info_changed,
  3737. .config_interface = b43_op_config_interface,
  3738. .configure_filter = b43_op_configure_filter,
  3739. .set_key = b43_op_set_key,
  3740. .get_stats = b43_op_get_stats,
  3741. .get_tx_stats = b43_op_get_tx_stats,
  3742. .start = b43_op_start,
  3743. .stop = b43_op_stop,
  3744. .set_tim = b43_op_beacon_set_tim,
  3745. .sta_notify = b43_op_sta_notify,
  3746. };
  3747. /* Hard-reset the chip. Do not call this directly.
  3748. * Use b43_controller_restart()
  3749. */
  3750. static void b43_chip_reset(struct work_struct *work)
  3751. {
  3752. struct b43_wldev *dev =
  3753. container_of(work, struct b43_wldev, restart_work);
  3754. struct b43_wl *wl = dev->wl;
  3755. int err = 0;
  3756. int prev_status;
  3757. mutex_lock(&wl->mutex);
  3758. prev_status = b43_status(dev);
  3759. /* Bring the device down... */
  3760. if (prev_status >= B43_STAT_STARTED)
  3761. b43_wireless_core_stop(dev);
  3762. if (prev_status >= B43_STAT_INITIALIZED)
  3763. b43_wireless_core_exit(dev);
  3764. /* ...and up again. */
  3765. if (prev_status >= B43_STAT_INITIALIZED) {
  3766. err = b43_wireless_core_init(dev);
  3767. if (err)
  3768. goto out;
  3769. }
  3770. if (prev_status >= B43_STAT_STARTED) {
  3771. err = b43_wireless_core_start(dev);
  3772. if (err) {
  3773. b43_wireless_core_exit(dev);
  3774. goto out;
  3775. }
  3776. }
  3777. out:
  3778. if (err)
  3779. wl->current_dev = NULL; /* Failed to init the dev. */
  3780. mutex_unlock(&wl->mutex);
  3781. if (err)
  3782. b43err(wl, "Controller restart FAILED\n");
  3783. else
  3784. b43info(wl, "Controller restarted\n");
  3785. }
  3786. static int b43_setup_bands(struct b43_wldev *dev,
  3787. bool have_2ghz_phy, bool have_5ghz_phy)
  3788. {
  3789. struct ieee80211_hw *hw = dev->wl->hw;
  3790. if (have_2ghz_phy)
  3791. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
  3792. if (dev->phy.type == B43_PHYTYPE_N) {
  3793. if (have_5ghz_phy)
  3794. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
  3795. } else {
  3796. if (have_5ghz_phy)
  3797. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
  3798. }
  3799. dev->phy.supports_2ghz = have_2ghz_phy;
  3800. dev->phy.supports_5ghz = have_5ghz_phy;
  3801. return 0;
  3802. }
  3803. static void b43_wireless_core_detach(struct b43_wldev *dev)
  3804. {
  3805. /* We release firmware that late to not be required to re-request
  3806. * is all the time when we reinit the core. */
  3807. b43_release_firmware(dev);
  3808. b43_phy_free(dev);
  3809. }
  3810. static int b43_wireless_core_attach(struct b43_wldev *dev)
  3811. {
  3812. struct b43_wl *wl = dev->wl;
  3813. struct ssb_bus *bus = dev->dev->bus;
  3814. struct pci_dev *pdev = bus->host_pci;
  3815. int err;
  3816. bool have_2ghz_phy = 0, have_5ghz_phy = 0;
  3817. u32 tmp;
  3818. /* Do NOT do any device initialization here.
  3819. * Do it in wireless_core_init() instead.
  3820. * This function is for gathering basic information about the HW, only.
  3821. * Also some structs may be set up here. But most likely you want to have
  3822. * that in core_init(), too.
  3823. */
  3824. err = ssb_bus_powerup(bus, 0);
  3825. if (err) {
  3826. b43err(wl, "Bus powerup failed\n");
  3827. goto out;
  3828. }
  3829. /* Get the PHY type. */
  3830. if (dev->dev->id.revision >= 5) {
  3831. u32 tmshigh;
  3832. tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
  3833. have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
  3834. have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
  3835. } else
  3836. B43_WARN_ON(1);
  3837. dev->phy.gmode = have_2ghz_phy;
  3838. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3839. b43_wireless_core_reset(dev, tmp);
  3840. err = b43_phy_versioning(dev);
  3841. if (err)
  3842. goto err_powerdown;
  3843. /* Check if this device supports multiband. */
  3844. if (!pdev ||
  3845. (pdev->device != 0x4312 &&
  3846. pdev->device != 0x4319 && pdev->device != 0x4324)) {
  3847. /* No multiband support. */
  3848. have_2ghz_phy = 0;
  3849. have_5ghz_phy = 0;
  3850. switch (dev->phy.type) {
  3851. case B43_PHYTYPE_A:
  3852. have_5ghz_phy = 1;
  3853. break;
  3854. case B43_PHYTYPE_G:
  3855. case B43_PHYTYPE_N:
  3856. have_2ghz_phy = 1;
  3857. break;
  3858. default:
  3859. B43_WARN_ON(1);
  3860. }
  3861. }
  3862. if (dev->phy.type == B43_PHYTYPE_A) {
  3863. /* FIXME */
  3864. b43err(wl, "IEEE 802.11a devices are unsupported\n");
  3865. err = -EOPNOTSUPP;
  3866. goto err_powerdown;
  3867. }
  3868. if (1 /* disable A-PHY */) {
  3869. /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
  3870. if (dev->phy.type != B43_PHYTYPE_N) {
  3871. have_2ghz_phy = 1;
  3872. have_5ghz_phy = 0;
  3873. }
  3874. }
  3875. err = b43_phy_allocate(dev);
  3876. if (err)
  3877. goto err_powerdown;
  3878. dev->phy.gmode = have_2ghz_phy;
  3879. tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
  3880. b43_wireless_core_reset(dev, tmp);
  3881. err = b43_validate_chipaccess(dev);
  3882. if (err)
  3883. goto err_phy_free;
  3884. err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
  3885. if (err)
  3886. goto err_phy_free;
  3887. /* Now set some default "current_dev" */
  3888. if (!wl->current_dev)
  3889. wl->current_dev = dev;
  3890. INIT_WORK(&dev->restart_work, b43_chip_reset);
  3891. dev->phy.ops->switch_analog(dev, 0);
  3892. ssb_device_disable(dev->dev, 0);
  3893. ssb_bus_may_powerdown(bus);
  3894. out:
  3895. return err;
  3896. err_phy_free:
  3897. b43_phy_free(dev);
  3898. err_powerdown:
  3899. ssb_bus_may_powerdown(bus);
  3900. return err;
  3901. }
  3902. static void b43_one_core_detach(struct ssb_device *dev)
  3903. {
  3904. struct b43_wldev *wldev;
  3905. struct b43_wl *wl;
  3906. /* Do not cancel ieee80211-workqueue based work here.
  3907. * See comment in b43_remove(). */
  3908. wldev = ssb_get_drvdata(dev);
  3909. wl = wldev->wl;
  3910. b43_debugfs_remove_device(wldev);
  3911. b43_wireless_core_detach(wldev);
  3912. list_del(&wldev->list);
  3913. wl->nr_devs--;
  3914. ssb_set_drvdata(dev, NULL);
  3915. kfree(wldev);
  3916. }
  3917. static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
  3918. {
  3919. struct b43_wldev *wldev;
  3920. struct pci_dev *pdev;
  3921. int err = -ENOMEM;
  3922. if (!list_empty(&wl->devlist)) {
  3923. /* We are not the first core on this chip. */
  3924. pdev = dev->bus->host_pci;
  3925. /* Only special chips support more than one wireless
  3926. * core, although some of the other chips have more than
  3927. * one wireless core as well. Check for this and
  3928. * bail out early.
  3929. */
  3930. if (!pdev ||
  3931. ((pdev->device != 0x4321) &&
  3932. (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
  3933. b43dbg(wl, "Ignoring unconnected 802.11 core\n");
  3934. return -ENODEV;
  3935. }
  3936. }
  3937. wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
  3938. if (!wldev)
  3939. goto out;
  3940. wldev->dev = dev;
  3941. wldev->wl = wl;
  3942. b43_set_status(wldev, B43_STAT_UNINIT);
  3943. wldev->bad_frames_preempt = modparam_bad_frames_preempt;
  3944. tasklet_init(&wldev->isr_tasklet,
  3945. (void (*)(unsigned long))b43_interrupt_tasklet,
  3946. (unsigned long)wldev);
  3947. INIT_LIST_HEAD(&wldev->list);
  3948. err = b43_wireless_core_attach(wldev);
  3949. if (err)
  3950. goto err_kfree_wldev;
  3951. list_add(&wldev->list, &wl->devlist);
  3952. wl->nr_devs++;
  3953. ssb_set_drvdata(dev, wldev);
  3954. b43_debugfs_add_device(wldev);
  3955. out:
  3956. return err;
  3957. err_kfree_wldev:
  3958. kfree(wldev);
  3959. return err;
  3960. }
  3961. #define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
  3962. (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
  3963. (pdev->device == _device) && \
  3964. (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
  3965. (pdev->subsystem_device == _subdevice) )
  3966. static void b43_sprom_fixup(struct ssb_bus *bus)
  3967. {
  3968. struct pci_dev *pdev;
  3969. /* boardflags workarounds */
  3970. if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
  3971. bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
  3972. bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
  3973. if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
  3974. bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
  3975. bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
  3976. if (bus->bustype == SSB_BUSTYPE_PCI) {
  3977. pdev = bus->host_pci;
  3978. if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
  3979. IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
  3980. IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
  3981. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
  3982. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
  3983. IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
  3984. IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
  3985. bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
  3986. }
  3987. }
  3988. static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
  3989. {
  3990. struct ieee80211_hw *hw = wl->hw;
  3991. ssb_set_devtypedata(dev, NULL);
  3992. ieee80211_free_hw(hw);
  3993. }
  3994. static int b43_wireless_init(struct ssb_device *dev)
  3995. {
  3996. struct ssb_sprom *sprom = &dev->bus->sprom;
  3997. struct ieee80211_hw *hw;
  3998. struct b43_wl *wl;
  3999. int err = -ENOMEM;
  4000. b43_sprom_fixup(dev->bus);
  4001. hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
  4002. if (!hw) {
  4003. b43err(NULL, "Could not allocate ieee80211 device\n");
  4004. goto out;
  4005. }
  4006. /* fill hw info */
  4007. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  4008. IEEE80211_HW_SIGNAL_DBM |
  4009. IEEE80211_HW_NOISE_DBM;
  4010. hw->wiphy->interface_modes =
  4011. BIT(NL80211_IFTYPE_AP) |
  4012. BIT(NL80211_IFTYPE_MESH_POINT) |
  4013. BIT(NL80211_IFTYPE_STATION) |
  4014. BIT(NL80211_IFTYPE_WDS) |
  4015. BIT(NL80211_IFTYPE_ADHOC);
  4016. hw->queues = b43_modparam_qos ? 4 : 1;
  4017. hw->max_rates = 2;
  4018. SET_IEEE80211_DEV(hw, dev->dev);
  4019. if (is_valid_ether_addr(sprom->et1mac))
  4020. SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
  4021. else
  4022. SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
  4023. /* Get and initialize struct b43_wl */
  4024. wl = hw_to_b43_wl(hw);
  4025. memset(wl, 0, sizeof(*wl));
  4026. wl->hw = hw;
  4027. spin_lock_init(&wl->irq_lock);
  4028. rwlock_init(&wl->tx_lock);
  4029. spin_lock_init(&wl->leds_lock);
  4030. spin_lock_init(&wl->shm_lock);
  4031. mutex_init(&wl->mutex);
  4032. INIT_LIST_HEAD(&wl->devlist);
  4033. INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
  4034. INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
  4035. ssb_set_devtypedata(dev, wl);
  4036. b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
  4037. err = 0;
  4038. out:
  4039. return err;
  4040. }
  4041. static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
  4042. {
  4043. struct b43_wl *wl;
  4044. int err;
  4045. int first = 0;
  4046. wl = ssb_get_devtypedata(dev);
  4047. if (!wl) {
  4048. /* Probing the first core. Must setup common struct b43_wl */
  4049. first = 1;
  4050. err = b43_wireless_init(dev);
  4051. if (err)
  4052. goto out;
  4053. wl = ssb_get_devtypedata(dev);
  4054. B43_WARN_ON(!wl);
  4055. }
  4056. err = b43_one_core_attach(dev, wl);
  4057. if (err)
  4058. goto err_wireless_exit;
  4059. if (first) {
  4060. err = ieee80211_register_hw(wl->hw);
  4061. if (err)
  4062. goto err_one_core_detach;
  4063. }
  4064. out:
  4065. return err;
  4066. err_one_core_detach:
  4067. b43_one_core_detach(dev);
  4068. err_wireless_exit:
  4069. if (first)
  4070. b43_wireless_exit(dev, wl);
  4071. return err;
  4072. }
  4073. static void b43_remove(struct ssb_device *dev)
  4074. {
  4075. struct b43_wl *wl = ssb_get_devtypedata(dev);
  4076. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4077. /* We must cancel any work here before unregistering from ieee80211,
  4078. * as the ieee80211 unreg will destroy the workqueue. */
  4079. cancel_work_sync(&wldev->restart_work);
  4080. B43_WARN_ON(!wl);
  4081. if (wl->current_dev == wldev)
  4082. ieee80211_unregister_hw(wl->hw);
  4083. b43_one_core_detach(dev);
  4084. if (list_empty(&wl->devlist)) {
  4085. /* Last core on the chip unregistered.
  4086. * We can destroy common struct b43_wl.
  4087. */
  4088. b43_wireless_exit(dev, wl);
  4089. }
  4090. }
  4091. /* Perform a hardware reset. This can be called from any context. */
  4092. void b43_controller_restart(struct b43_wldev *dev, const char *reason)
  4093. {
  4094. /* Must avoid requeueing, if we are in shutdown. */
  4095. if (b43_status(dev) < B43_STAT_INITIALIZED)
  4096. return;
  4097. b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
  4098. queue_work(dev->wl->hw->workqueue, &dev->restart_work);
  4099. }
  4100. #ifdef CONFIG_PM
  4101. static int b43_suspend(struct ssb_device *dev, pm_message_t state)
  4102. {
  4103. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4104. struct b43_wl *wl = wldev->wl;
  4105. b43dbg(wl, "Suspending...\n");
  4106. mutex_lock(&wl->mutex);
  4107. wldev->suspend_in_progress = true;
  4108. wldev->suspend_init_status = b43_status(wldev);
  4109. if (wldev->suspend_init_status >= B43_STAT_STARTED)
  4110. b43_wireless_core_stop(wldev);
  4111. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
  4112. b43_wireless_core_exit(wldev);
  4113. mutex_unlock(&wl->mutex);
  4114. b43dbg(wl, "Device suspended.\n");
  4115. return 0;
  4116. }
  4117. static int b43_resume(struct ssb_device *dev)
  4118. {
  4119. struct b43_wldev *wldev = ssb_get_drvdata(dev);
  4120. struct b43_wl *wl = wldev->wl;
  4121. int err = 0;
  4122. b43dbg(wl, "Resuming...\n");
  4123. mutex_lock(&wl->mutex);
  4124. if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
  4125. err = b43_wireless_core_init(wldev);
  4126. if (err) {
  4127. b43err(wl, "Resume failed at core init\n");
  4128. goto out;
  4129. }
  4130. }
  4131. if (wldev->suspend_init_status >= B43_STAT_STARTED) {
  4132. err = b43_wireless_core_start(wldev);
  4133. if (err) {
  4134. b43_leds_exit(wldev);
  4135. b43_rng_exit(wldev->wl);
  4136. b43_wireless_core_exit(wldev);
  4137. b43err(wl, "Resume failed at core start\n");
  4138. goto out;
  4139. }
  4140. }
  4141. b43dbg(wl, "Device resumed.\n");
  4142. out:
  4143. wldev->suspend_in_progress = false;
  4144. mutex_unlock(&wl->mutex);
  4145. return err;
  4146. }
  4147. #else /* CONFIG_PM */
  4148. # define b43_suspend NULL
  4149. # define b43_resume NULL
  4150. #endif /* CONFIG_PM */
  4151. static struct ssb_driver b43_ssb_driver = {
  4152. .name = KBUILD_MODNAME,
  4153. .id_table = b43_ssb_tbl,
  4154. .probe = b43_probe,
  4155. .remove = b43_remove,
  4156. .suspend = b43_suspend,
  4157. .resume = b43_resume,
  4158. };
  4159. static void b43_print_driverinfo(void)
  4160. {
  4161. const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
  4162. *feat_leds = "", *feat_rfkill = "";
  4163. #ifdef CONFIG_B43_PCI_AUTOSELECT
  4164. feat_pci = "P";
  4165. #endif
  4166. #ifdef CONFIG_B43_PCMCIA
  4167. feat_pcmcia = "M";
  4168. #endif
  4169. #ifdef CONFIG_B43_NPHY
  4170. feat_nphy = "N";
  4171. #endif
  4172. #ifdef CONFIG_B43_LEDS
  4173. feat_leds = "L";
  4174. #endif
  4175. #ifdef CONFIG_B43_RFKILL
  4176. feat_rfkill = "R";
  4177. #endif
  4178. printk(KERN_INFO "Broadcom 43xx driver loaded "
  4179. "[ Features: %s%s%s%s%s, Firmware-ID: "
  4180. B43_SUPPORTED_FIRMWARE_ID " ]\n",
  4181. feat_pci, feat_pcmcia, feat_nphy,
  4182. feat_leds, feat_rfkill);
  4183. }
  4184. static int __init b43_init(void)
  4185. {
  4186. int err;
  4187. b43_debugfs_init();
  4188. err = b43_pcmcia_init();
  4189. if (err)
  4190. goto err_dfs_exit;
  4191. err = ssb_driver_register(&b43_ssb_driver);
  4192. if (err)
  4193. goto err_pcmcia_exit;
  4194. b43_print_driverinfo();
  4195. return err;
  4196. err_pcmcia_exit:
  4197. b43_pcmcia_exit();
  4198. err_dfs_exit:
  4199. b43_debugfs_exit();
  4200. return err;
  4201. }
  4202. static void __exit b43_exit(void)
  4203. {
  4204. ssb_driver_unregister(&b43_ssb_driver);
  4205. b43_pcmcia_exit();
  4206. b43_debugfs_exit();
  4207. }
  4208. module_init(b43_init)
  4209. module_exit(b43_exit)