main.c 134 KB

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