main.c 134 KB

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