main.c 125 KB

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